Executing Multiple Functions Simultaneously

Executing multiple functions simultaneously

You are doing it correctly. :)

Try running this silly piece of code:

from multiprocessing import Process
import sys

rocket = 0

def func1():
global rocket
print 'start func1'
while rocket < sys.maxint:
rocket += 1
print 'end func1'

def func2():
global rocket
print 'start func2'
while rocket < sys.maxint:
rocket += 1
print 'end func2'

if __name__=='__main__':
p1 = Process(target = func1)
p1.start()
p2 = Process(target = func2)
p2.start()

You will see it print 'start func1' and then 'start func2' and then after a (very) long time you will finally see the functions end. But they will indeed execute simultaneously.

Because processes take a while to start up, you may even see 'start func2' before 'start func1'.

How to have two functions running simultaneously with python?

One of your options is to use the asyncio module.

First of all, import the asyncio module with import asyncio. Replace your def functions to async def functions.

Second of all, replace time.sleep() with await asyncio.sleep(). You no longer need the time module because of this step.

Thirdly, create a new function, normally called main(). You can take the following code snippet for reference:

async def main():
task1 = asyncio.create_task(
short_task())
task2 = asyncio.create_task(
long_task())
await task1
await task2

Finally, run the whole main() code with asyncio.run(main()). Your final code should look like this:

import asyncio
async def short_task():
await asyncio.sleep(2)

async def long_task():
await asyncio.sleep(4)

async def main():
task1 = asyncio.create_task(
short_task())
task2 = asyncio.create_task(
long_task())
await task1
await task2

You may use a simple code snippet to proof that the whole process took 4 seconds.

Python: Ending multiple functions simultaneously

You can wait for p1 to finish with p1.join then terminate p2.

p1 = Process(target = func1)
p2 = Process(target = func2)
p1.start()
p2.start()

p1.join()
p2.terminate()
p2.join()

how to run multiple functions at same time

you can use multiprocessing or threading
threading in python doesnt actually run in parallel but allow you to run the functions at the same time (and python will iterate them, doing a few lines from each at a time)

with multiprocessing they WILL run in parallel (assuming you have multiple cpu cores) but they wont share memory.
here is a sample with multiprocessing

from multiprocessing import Process
p = Process(target=myfunc1)
p.start()
p2 = Process(target=myfunc2)
p2.start()
# and so on
p.join()
p2.join()
# the join means wait untill it finished

you can read more about it here:

https://docs.python.org/2/library/multiprocessing.html

https://wiki.python.org/moin/GlobalInterpreterLock

Make 2 functions run at the same time

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()


Related Topics



Leave a reply



Submit