- 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 areconcurrent
(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
andsuspend
. 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, cansend(para)
to generator(Receiver) e.g.para = yield
- yield: It is
not coroutine
, is a generator
- mainly for data pipeline
- when calls
next()
, it generates the value at yield expression and then suspends until a next call `next()
CPU-bound
-> multi-processingIO-bound
(network/read and write) -> 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
-
socket:
ip+port
concept and offer the abstract methods.e.g. send(),recv(),connect(),close()
-
protocol: TCP / UDP / HTTP / Customized
- socket programming by asyncio