协程中调用阻塞函数

代码演示:

 1 from tornado import gen
 2 from concurrent.futures import ThreadPoolExecutor
 3 
 4 thread_pool = ThreadPoolExecutor(2)
 5 
 6 
 7 def my_sleep(count):
 8     import time
 9     for i in range(count):
10         time.sleep(1)
11     print("*"*30)
12     return "my_sleep函数返回"
13 
14 
15 @gen.coroutine
16 def call_blocking():
17     print("开始call_blocking...")
18     yield thread_pool.submit(my_sleep, 10)
19     print("end of call_blocking...")
20 
21 
22 if __name__ == '__main__':
23     print(call_blocking())
24     print("调试程序...")

执行结果:

  

这里主进程的执行大家可以很清楚的,调用函数的执行在打印结果中的30个*号得以展示。

 

posted @ 2018-06-11 11:32  巴蜀秀才  阅读(484)  评论(0编辑  收藏  举报