Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make 2 functions run at the same time

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?

like image 320
John Avatar asked Jun 02 '10 11:06

John


People also ask

How do you run two functions at the same time?

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.

Can Python run two functions in parallel?

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.

How do I run two threads simultaneously in Python?

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.


1 Answers

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() 
like image 194
chrisg Avatar answered Oct 14 '22 00:10

chrisg