Python-Advance
Argvs
Reference
Decorator
Reference
- Python進階技巧 (3) — 神奇又美好的 Decorator ,嗷嗚! | by Jack Cheng | 整個程式都是我的咖啡館 | Medium
- Python 的 staticmethod 與 classmethod | by 莊子弘 | Medium
Asynchronous
Basiclly, the asynchronous programming can discribe in the following concept:
- Event Loop
- Event
- CallBack
Event Loop
Since the asynchronous process
must swich between different tasks. There must be a list to maintain all the tasks and info, which is what a Event loop doing.
Event and CallBack
If there is a asynchronous process, it must register to a Eventloop like Event : CallBack
, then the EventLoop would iterate throught the registered process like for
doing.
If a Event is happening, the EventLoop would invoke the CallBack, and stop listening the event.
Take the Following Code for Example:
import asyncio
loop = asyncio.get_event_loop() # create a Event Loop
async def example1(): # Define a coroutine that would be interrup
print("Start example1 coroutin.")
await asyncio.sleep(1) # inturrup the coroutine for 1 second
print("Finish example1 coroutin.")
async def example2(): # Define a coroutine
print("Start example2 coroutin.")
# do some process...
print("Finish example2 coroutin.")
tasks = [ # create a list of task
asyncio.ensure_future(example1()),
asyncio.ensure_future(example2()),
]
# register the two corotinue to EventLoop
# loop start, exec example1, pause example, exec example2, resume example1
loop.run_until_complete(asyncio.wait(tasks))
# output:
# Start example1 coroutin.
# Start example2 coroutin.
# Finish example2 coroutin.
# Finish example1 coroutin.
Coroutine
A coroutine is a function that can be paused, returned, and resumed in the halfway.
For Python Package asycnio
, a corotinue can be declare by adding async
in front of the function, like async def example():
Task
To put it simply, a coroutine must be encapencapsulated to a task to communicate with EventLoop.
For example, the line loop.run_until_complete(example())
would convert the coroutine example()
into a task, then register to EventLoop.
Await
If there were no await
, the coroutine would have executed the counter directly, instead of pausing. One can think await
as a declaration of callback function. One can relate the concept to the picture(2)(3) above.