English
Web appOpen in Telegram

ThreadHey, i got a sync function that i want to call asyncronously in a loop

7 messages · –
G
Hey, i got a sync function that i want to call asyncronously in a loop def api_call() ... # Do api call for chunk in batched(parse_csv(local_path), BATCH_LIMIT): # Do 10 api calls in parallel using async I tried with different things but couldn't get anything working. How do you do it?
  1. C
    Don't do it. Running a synchronous function in async context breaks your loop in a way that the other concurrent functions won't run until you're done. This is likely what you have experienced when trying to make it work. Run the code in a different thread, that puts it outside of the context of the asynchronous loop. Try asyncio.to_thread and asyncio.gather.
    1. A
      I think the question is more like, I have a sync function, how do I parallel-ize it such that I can handle it like an async function without doing the threads etc. stuff myself.
      1. C
        asyncio.to_thread is the abstraction. The answer only changes if the synchronous function is developer-owned and can be converted to a native asynchronous function. Unless of course there's an option to just pick an asynchronous library (assuming the synchronous function comes from a library) to replace the synchronous function.
PPython TalkPython Talk@python_talk · group · Tech
58members37 339messages in the index
Venue feed Open in Telegram

An open public feed from the search index ChatCrawler — “Google for public Telegram”; refreshed as the venue is crawled. Times are UTC.

Public content only, official Telegram API. About · FAQ · Index limits · Remove a page · Catalog · Search · How we count