Skip to content

Instantly share code, notes, and snippets.

@demiurg
Created March 28, 2024 21:49
Show Gist options
  • Save demiurg/4b3ae4d827d8f45b80ccc135a1661a75 to your computer and use it in GitHub Desktop.
Save demiurg/4b3ae4d827d8f45b80ccc135a1661a75 to your computer and use it in GitHub Desktop.
test_async_approaches.py
from concurrent.futures import ThreadPoolExecutor, wait
from multiprocessing import cpu_count
import asyncio
def proc(arg):
print(arg)
async def aproc(arg):
print(arg)
def main(args):
executor = ThreadPoolExecutor(max_workers=cpu_count())
futures = [executor.submit(proc, i) for i in args]
completed, pending = wait(futures)
print("done main", len(completed), "/", len(pending))
async def amain(args):
tasks = []
for arg in args:
tasks.append(asyncio.create_task(aproc(arg)))
results = await asyncio.gather(*tasks)
print("done async main", len(results))
if __name__ == "__main__":
args = range(10)
main(args)
asyncio.run(amain(args))
@demiurg
Copy link
Author

demiurg commented Mar 28, 2024

  • ThreadPoolExecutor
    • This allows to switch between thread pool, and process pool
    • Control number of parallel executions
    • Allows some control on blocking
    • does not color functions, a single thread is a single thread
  • async/await
    • This allows to "nest" all the async colored functions, potentially more performant if multiple functions do IO
    • colored functions, can call sync code, but async functions can not be reused in sync contest
    • asyncio event loop does not rely on threading and is most performant even for smaller python tasks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment