0%

python | 异步、多线程相互纠缠

暂不涉及多进程。

在很多场景,协程是和多线程相互纠缠的

  • 协程中运行多线程
    • python | await | 同步 && 异步?
    • 在上面的章节中,我们了解到如果协程中执行非异步代码,会进行阻塞
    • 但是,我们有时候想执行非异步代码的时候,也不阻塞
  • 多线程中的协程
    • 这种场景非常有用
    • 我用于数据获取的实时性

协程中运行多线程

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
import asyncio
import time


async def run0():
loop = asyncio.get_event_loop()
loop.run_in_executor(None, time.sleep, 1)
print("run0")


async def run1():
loop = asyncio.get_event_loop()
loop.run_in_executor(None, time.sleep, 2)
print("run1")


if __name__ == '__main__':
now = time.time()
tasks = [
run0(),
run1()
]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
print(time.time() - now)

输出

1
2
3
run1
run0
0.002712249755859375

但是,很明显不对,所以要改成下面这个样子。

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
import asyncio
import time


async def run0():
loop = asyncio.get_event_loop()
f = loop.run_in_executor(None, time.sleep, 1)
await f
print("run0")


async def run1():
loop = asyncio.get_event_loop()
f = loop.run_in_executor(None, time.sleep, 2)
await f
print("run1")


if __name__ == '__main__':
now = time.time()
tasks = [
run0(),
run1()
]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
print(time.time() - now)

输出

1
2
3
run0
run1
2.009390115737915

多线程运行协程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import asyncio
from concurrent.futures import ThreadPoolExecutor, as_completed
import time

pools = ThreadPoolExecutor(2)


async def run0():
await asyncio.sleep(2)
print("success")


def thread_start():
asyncio.run(asyncio.wait([run0()]))


now = time.time()
for i in range(0, 2):
pools.submit(thread_start)
print(time.time() - now)

输出

1
2
3
0.0021479129791259766
success
success
请我喝杯咖啡吧~