Skip to content

Instantly share code, notes, and snippets.

@Kiollpt
Last active September 20, 2020 03:29
Show Gist options
  • Save Kiollpt/34980d9cf1a0c470858406461257dc7c to your computer and use it in GitHub Desktop.
Save Kiollpt/34980d9cf1a0c470858406461257dc7c to your computer and use it in GitHub Desktop.
#asyncio

reference

  1. https://juejin.im/post/6844903632534503437

  2. https://www.educative.io/blog/python-concurrency-making-sense-of-asyncio

  3. https://realpython.com/async-io-python/#async-io-explained

  4. A good point why uses third party of asyncio packages

Term

  • async vs Threading : async is executed in single thread and switch context by YOU. It is more lightway Threading will have race condition and hard to debug beacaus Threading switchs context by CPU Thread and async are concurrent(have overlapping task), but threads are likely to execute on different cores will be multi-processing
    (Parallel)
  • async def f : define async function
  • coroutine object : can resume and suspend. e.g f()
  • return in the coroutine: this coroution is finished
  • task: schedule the corotuine to the event loop, and is subclass of Future
  • await: like yield from, which is yield control back to the other corotuine until this corotuine is compeleted
  • future: means on progress or will be schedule later. support functions are include set_result()
  • yield from like delegator: It is generator-based coroutine fun, can send(para) to generator(Receiver) e.g. para = yield
  • yield: It is not coroutine, is a generator

chaining coroutine

  • mainly for data pipeline

generator

  • when calls next(), it generates the value at yield expression and then suspends until a next call `next()

Summary

  • CPU-bound-> multi-processing
  • IO-bound(network/read and write) -> asyncio

Comparison of Threading and asyncio

  • Both of them have an idea to run tasks concurrently. But asyncio subtly run tasks overlapped by resume and suspend(non-blocking). while Threading run tasks all from beginning (blocking).
  • Threading is hard to trace bugs / CPU swithch CONTEXT / race condition / not scaleable well

Third party Libraries

ZeroMQ

  • socket programming by asyncio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment