How to run two functions at the same time with an object?

Prox

So I found an example that shows

import threading
from threading import Thread

def func1():
    print 'Working'

def func2():
    print 'Working'

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = func2).start()

and this works.

But how does it work when I usually have an object with the function. Here are 2 functions I normally use.

statusbar()
copyfrom(servername.strip())

How do I add servername.strip() to the below?

Thread(target = statusbar).start()
Thread(target = copyfrom).start()
SwiftsNamesake

The target should be a function, so define your own function that does what you want:

def doSomething():
  copyfrom(servername.strip())

and hand it to your Thread.

Thread(target=doSomething).start()

If you want to do it in a single line, you could use an anonymous function:

Thread(target=lambda: copyfrom(servername.strip())).start()

As cᴏʟᴅsᴘᴇᴇᴅ mentions in his answer, you can also specify arguments for the callback as a tuple:

Thread(target=copyfrom, args=(servername.strip(), )).start()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to start two functions at the same time and only wait for the faster one?

From Dev

two ajax functions at the same time? not working

From Dev

Run two functions in the exact same time

From Dev

Run two functions at the same time

From Dev

Build/Run two targets in Xcode at the same time

From Dev

Test while running two functions at the same time

From Dev

How to let cron run in two separate time frames on the same day?

From Dev

Run Two Instances Of vlc At The Same Time

From Dev

Python - run two commands at the same time

From Dev

How to run two copies of xampp at the same time?

From Dev

how to run two while loops at the same time in Python?

From Dev

how to run multiple functions at same time

From Dev

How to test if two functions are the same?

From Dev

How are these two functions the same?

From Dev

WPF: Run Validator on two comboboxes as the same time

From Dev

Run two commands at the same time in Elm

From Dev

Run two Python functions at the same time

From Dev

How do I run two different conky sessions at the same time?

From Dev

How can I run two different main class at the same time?

From Dev

Run two functions in the exact same time

From Dev

Test while running two functions at the same time

From Dev

Python - run two commands at the same time

From Dev

How would one run two controllers at the same time AngularJS

From Dev

two functions accessing database at the same time

From Dev

How to run two X sessions at the same time?

From Dev

How to run at the same time two or more docker-compose files

From Dev

How to run two cases with different functions at the same time?

From Dev

How to start and run two background services at the same time?

From Dev

How Do I Run Two Different Specs at The Same Time?

Related Related

  1. 1

    How to start two functions at the same time and only wait for the faster one?

  2. 2

    two ajax functions at the same time? not working

  3. 3

    Run two functions in the exact same time

  4. 4

    Run two functions at the same time

  5. 5

    Build/Run two targets in Xcode at the same time

  6. 6

    Test while running two functions at the same time

  7. 7

    How to let cron run in two separate time frames on the same day?

  8. 8

    Run Two Instances Of vlc At The Same Time

  9. 9

    Python - run two commands at the same time

  10. 10

    How to run two copies of xampp at the same time?

  11. 11

    how to run two while loops at the same time in Python?

  12. 12

    how to run multiple functions at same time

  13. 13

    How to test if two functions are the same?

  14. 14

    How are these two functions the same?

  15. 15

    WPF: Run Validator on two comboboxes as the same time

  16. 16

    Run two commands at the same time in Elm

  17. 17

    Run two Python functions at the same time

  18. 18

    How do I run two different conky sessions at the same time?

  19. 19

    How can I run two different main class at the same time?

  20. 20

    Run two functions in the exact same time

  21. 21

    Test while running two functions at the same time

  22. 22

    Python - run two commands at the same time

  23. 23

    How would one run two controllers at the same time AngularJS

  24. 24

    two functions accessing database at the same time

  25. 25

    How to run two X sessions at the same time?

  26. 26

    How to run at the same time two or more docker-compose files

  27. 27

    How to run two cases with different functions at the same time?

  28. 28

    How to start and run two background services at the same time?

  29. 29

    How Do I Run Two Different Specs at The Same Time?

HotTag

Archive