The project delved into asynchronous programming in Python, covering async and await syntax. It also explained how to execute an async program using asyncio, run concurrent coroutines, create asyncio tasks, and utilize the random module for various applications.
In summary, the project provided a comprehensive understanding of asynchronous programming techniques and their practical implementation in Python.
Read or watch:
General
- A mandatory README.md file, at the root of the folder of the project.
- Allowed editors:
vi,vim,emacs - All files will be interpreted/compiled on Ubuntu 18.04 LTS using
python3(version 3.7) - All files should end with a new line
- All files must be executable
- The length of the files should be tested using
wc - The first line of all files should be exactly
#!/usr/bin/env python3 - The code should use the
pycodestylestyle (version 2.5.x) - All functions and coroutines must be type-annotated.
- All modules should have a documentation (
python3 -c 'print(__import__("my_module").__doc__)') - All functions should have a documentation (
python3 -c 'print(__import__("my_module").my_function.__doc__)') - A documentation is not a simple word, it’s a real sentence explaining what’s the purpose of the module, class or method (the length of it will be verified)
0. The basics of async
- 0-basic_async_syntax.py: An asynchronous coroutine that takes in an integer argument (
max_delay, with a default value of10) namedwait_randomthat waits for a random delay between0andmax_delay(included and float value) seconds and eventually returns it.- It uses the
randommodule.
- It uses the
1. Let's execute multiple coroutines at the same time with async
- 1-concurrent_coroutines.py: An async routine called
wait_nthat importswait_randomfrom the0-basic_async_syntaxand takes in 2 int arguments (in this order):nandmax_delay, spawnswait_randomntimes with the specifiedmax_delay.wait_nreturns the list of all the delays (float values) in ascending order without usingsort()because of concurrency.
2. Measure the runtime
- 2-measure_runtime.py:
measure_timefunction with integersnandmax_delayas arguments that measures the total execution time forwait_n(n, max_delay), and returnstotal_time / n.- It imports
wait_nfrom1-concurrent_coroutinesmodule - it returns a float.
- it uses the
timemodule to measure an approximate elapsed time.
- It imports
3. Tasks
- 3-tasks.py:
task_wait_randomfunction (not an async function, it uses the regular function syntax) that takes an integermax_delayand returns aasyncio.Task.- it imports
wait_randomfrom0-basic_async_syntaxmodule
- it imports
4. Tasks
- 4-tasks.py: A modified wait_n function
task_wait_nthat callstask_wait_random.- it nearly identical to
wait_nexcept task_wait_random is being called.
- it nearly identical to
- tests: A directory that contain main files for testing purpose
👍