I am trying to make 2 functions run at the same time.
def func1(): print 'Working' def func2(): print 'Working' func1() func2()
Does anyone know how to do this?
Another way to run multiple functions simultaneously is to use the multiprocessing module, which allows you to create “processes” that will execute independently of each other. Finally, you can also use the asyncio module to run multiple functions concurrently.
One of Python's main weaknesses is its inability to have true parallelization due to the Global Interpreter Lock. However, some functions can still virtually run in parallel. Python allows this with two different concepts: multithreading and multiprocessing.
To implement threading in Python, you have to perform three steps: Inherit the class that contains the function you want to run in a separate thread by using the Thread class. Name the function you want to execute in a thread run() . Call the start() function from the object of the class containing the run() method.
Do this:
from threading import Thread def func1(): print('Working') def func2(): print("Working") if __name__ == '__main__': Thread(target = func1).start() Thread(target = func2).start()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With