0

I have a program that uses python threads like this: starting 5 threads and start processing a list of elements.

def process_element(element):
   print(element)

jobs = Queue()
    
def do_stuff(q):
    while not q.empty():
        value = q.get()
        process_element(element=value)
        q.task_done()
 
for i in line: # my list of element
    jobs.put(i)
    
for i in range(5):
    worker = threading.Thread(target=do_stuff, args=(jobs))
    worker.start()

jobs.join()

How can I use asyncio to do the same work.

2
  • What have you tried so far? Where exactly do you struggle? Commented Dec 28, 2020 at 11:39
  • when i try to migrate to asyncio i don't know how to translate my code to it Commented Dec 28, 2020 at 11:49

1 Answer 1

1

A literal translation of your code into asyncio would look like this:

import asyncio, random

async def process_element(element):
    print('starting', element)
    await asyncio.sleep(random.random())  # simulate IO-bound processing
    print('done', element)

async def do_stuff(q):
    while not q.empty():
        value = await q.get()
        await process_element(element=value)
        q.task_done()

async def main():
    jobs = asyncio.Queue()

    for i in range(20):
        await jobs.put(i)

    for i in range(5):
        asyncio.create_task(do_stuff(jobs))

    await jobs.join()

asyncio.run(main())

Note, however, that:

  • Asyncio handles IO-bound tasks such as communicating with HTTP servers or remote databases, chat servers, etc. It doesn't handle CPU-bound tasks, where a long-running operation will block the whole event loop. (In such cases multithreading or multiprocessing are more appropriate.) For this reason "converting" multithreaded code to asyncio often fails.

  • while not q.empty(): ... process ... is an anti-pattern in both threading and asyncio because it doesn't allow waiting for something to arrive in the queue. If you know all your items in advance, you don't need a queue in the first place, you could use ordinary lists.

Sign up to request clarification or add additional context in comments.

1 Comment

yes I know all my element before starting the process

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.