-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.py
More file actions
39 lines (25 loc) · 710 Bytes
/
context.py
File metadata and controls
39 lines (25 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import asyncio
from contextlib import AbstractAsyncContextManager, asynccontextmanager
@asynccontextmanager
async def lock_context():
print("before context 1")
yield
print("after context 1")
lock = asyncio.Lock()
@asynccontextmanager
async def acquire_context():
try:
async with lock:
print("acquiring context")
yield
print("releasing context")
except ValueError:
print("something went wrong")
raise
print("after context1 is finished")
async def run_task():
print("init")
async with acquire_context():
print("running task")
print("done")
asyncio.run(asyncio.gather())