Skip to content

Commit 0e06c04

Browse files
authored
Async/Await examples (#9)
* add * pytest verbosity
1 parent 17c5d2d commit 0e06c04

File tree

13 files changed

+91
-1
lines changed

13 files changed

+91
-1
lines changed

.github/workflows/python-app.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ jobs:
3636
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
3737
- name: Test with pytest
3838
run: |
39-
pytest
39+
pytest -vv

src/asynchronous/__init__.py

Whitespace-only changes.

src/asynchronous/runner/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Source: https://www.geeksforgeeks.org/python/await-in-python/
3+
"""
4+
import asyncio
5+
from asynchronous.runner.task.myasynctask import custom_async_task
6+
from asynchronous.runner.operation.myoperations import first_operation, second_operation
7+
8+
# this coroutine runs both runner concurrently with asyncio.gather()
9+
async def running_concurrently():
10+
await asyncio.gather(custom_async_task(first_operation, 3),
11+
custom_async_task(second_operation, 2))
12+
13+
# starts the event loop, running and waiting for both runner to complete
14+
def main():
15+
asyncio.run(running_concurrently())

src/asynchronous/runner/operation/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
Definition of two simple operations.
3+
"""
4+
5+
def first_operation():
6+
print("First operation")
7+
8+
def second_operation():
9+
print("Second operation")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Source: https://www.geeksforgeeks.org/python/await-in-python/
3+
"""
4+
import asyncio
5+
from asynchronous.runner.task.myasynctask import custom_async_task
6+
from asynchronous.runner.operation.myoperations import first_operation, second_operation
7+
8+
# runs two runner sequentially, waiting for one to finish before starting the next
9+
async def running_sequentially():
10+
await custom_async_task(first_operation, 3)
11+
await custom_async_task(second_operation, 2)
12+
13+
# executes both runner, totaling around 5 seconds of runtime
14+
def main():
15+
asyncio.run(running_sequentially())

src/asynchronous/runner/task/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import asyncio
2+
3+
async def custom_async_task(func, sleep_time):
4+
"""
5+
Custom asynchronous task (coroutine)
6+
:param func: operation to execute
7+
:param sleep_time: sleep time
8+
"""
9+
print(f"Execution of {func.__name__} started")
10+
func()
11+
await asyncio.sleep(sleep_time)
12+
print(f"Execution of {func.__name__} completed")

src/asynchronous/tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)