asyncio
Native method
- native method
get_config_async
:
| from config import ConfigClient
cc = ConfigClient(app_name='foo', label='main')
await cc.get_config_async(timeout=5.0)
cc.config
|
For python 3.8 > use asyncio REPL. python -m asyncio
If for some reason the get_config_async method cannot be used, there are still some other possibilities, such as:
This is important to avoid blocking the event loop.
asyncio.to_thread
| from config.spring import ConfigClient
cc = ConfigClient(app_name='foo', label='main')
await asyncio.to_thread(cc.get_config)
print(cc.config)
|
run_in_executor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 | import asyncio
import concurrent.futures
from config import ConfigClient
loop = asyncio.get_event_loop()
c1 = ConfigClient(app_name='foo', label='main')
c2 = ConfigClient(app_name='foo', label='main', profile='dev,docker,cloud')
# 1. default loop's executor
loop.run_in_executor(None, c1.get_config) # default thread pool
# 2. custom thread pool
with concurrent.futures.ThreadPoolExecutor() as pool:
await loop.run_in_executor(pool, c2.get_config)
print('- first client config -')
print(c1.config)
print('- second client config -')
print(c2.config)
|