1

please help me to find out where is the mistake, I want to run two functions in parallel. THNX

import asyncio
import time

async def test1():
    for i in range(10):
        print(f"First function {i}")

async def test2():
    for i in range(10):
        print(f"Second function {i}")

async def main():
    print(f"started at {time.strftime('%X')}")
    F = asyncio.create_task(test1())
    S = asyncio.create_task(test2())
    tasks = (F, S)

    await asyncio.gather(*tasks)
    print(f"finished at {time.strftime('%X')}")

asyncio.run(main())

Instead I get such an output that doesn't have anything relative to the parallel execution :

    started at 10:42:53
First function 0
First function 1
First function 2
First function 3
First function 4
First function 5
First function 6
First function 7
First function 8
First function 9
Second function 0
Second function 1
Second function 2
Second function 3
Second function 4
Second function 5
Second function 6
Second function 7
Second function 8
Second function 9
finished at 10:42:53
  • Your async functions do not await anything. That means they never yield control back to the event loop. That's why they run synchronously. You could add `await asyncio.sleep(0)`, but if your tasks are all CPU-bound, something like `multiprocessing` or `concurrent.futures.ProcessPoolExecutor` will provide better value than asyncio. – dirn Jun 24 '21 at 14:53

3 Answers3

1

Ok, finally I've rewrite my code to work with threads, and it works perfectly, but I couldn't figure out how to catch return values from threaded functions, so at first my code looked like this:

import threading
import time

def test1():
    for i in range(10):
        print(f"First function {i}")
    return 11

def test2():
    for i in range(10,20):
        print(f"Second function {i}")

def main():
    t = time.time()
    t1 = threading.Thread(target=test1)
    t2 = threading.Thread(target=test2)
    t1.start()
    t2.start()
    print(t1.join())
    t2.join()
    print("Woaahh!! My work is finished..")
    print("I took " + str(time.time() - t))

main() 

And wwith help of this thread I've changed a bit to handle return values, here

So now my code look like this:

import time
import concurrent.futures

def test1():
    for i in range(10):
        print(f"First function {i}")
    return 11

def test2():
    for i in range(10,20):
        print(f"Second function {i}")
    return 12

def main():
    t = time.time()
 
    with concurrent.futures.ThreadPoolExecutor() as executor:
        future = executor.submit(test1)
        future2 = executor.submit(test2)
        return_value = future.result()
        rv= future2.result()
        print(return_value)
        print(rv)
        
        print("Woaahh!! My work is finished..")
        print("I took " + str(time.time() - t))

main()
0

I do not know what is your problem in your code but if you wan to run in parallel i think you can try something like this, by using thread ?

threading.Thread(target=test1).start()
threading.Thread(target=test2).start()

https://docs.python.org/fr/3/library/threading.html

Tomo
  • 71
  • 1
  • 7
0

Here is the other example of handling functions output code using Queues

import threading
import time
import queue

def test1(q):
    for i in range(10):
        print(f"First function {i}")
    q.put(1)

def test2(q):
    for i in range(10,20):
        print(f"Second function {i}")
    q.put(2)

def main():
    q = queue.Queue()
    t = time.time()
    t1 = threading.Thread(target=test1, args=(q, ))
    t2 = threading.Thread(target=test2, args=(q, ))
    t1.start()
    t2.start()
    print(t1.join())
    t2.join()
    print("Woaahh!! My work is finished..")
    print("I took " + str(time.time() - t))

    while not q.empty():
        print(q.get())

main()