-
Notifications
You must be signed in to change notification settings - Fork 1
/
asyncio.py
61 lines (50 loc) · 2.01 KB
/
asyncio.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Context propagation examples when dealing with single-threaded
asyncio loops. It uses the Python 3.5+ syntax for simplicity.
"""
import asyncio
from ext import tracer
def coroutine_continue_propagation():
"""The asyncio loop executes two chained coroutines; the second
traced coroutine is a child of the first coroutine even if a
cooperative yield happens.
"""
async def do_async_work():
# executed in the main thread
with tracer.start_active_span(operation_name='some_work'):
# do some IO-bound work
pass
async def execute_job():
# executed in the main thread
with tracer.start_active_span(operation_name='execute_job'):
await do_async_work()
# ...do more work in this loop...
loop = asyncio.get_event_loop()
future = loop.create_task(execute_job())
loop.run_until_complete(future)
def coroutine_with_callbacks():
"""The asyncio loop executes two chained coroutines with the
second one that expects a success callback (that is also a
coroutine). The callback that may be scheduled later because of
two cooperative yields, retrieves the right ActiveSpan, setting
a tag and finishing the Span.
"""
async def success():
span = tracer.active_span
assert span is not None
span.set_tag('result', 'success')
span.finish()
async def do_async_work(cb_success):
# executed in the main thread; do some IO-bound work
span = tracer.start_active_span(operation_name='some_work')
is_success = True
if is_success:
await cb_success()
# ...do more work that is not traced in some_work span...
async def execute_job():
# executed in the main thread
with tracer.start_active_span(operation_name='execute_job'):
await do_async_work(success)
# ...do more work in this loop...
loop = asyncio.get_event_loop()
future = loop.create_task(execute_job())
loop.run_until_complete(future)