python多线程实现异步
import timefrom threading import Threaddef long_io(cb): def func(callback): print("开始耗时操作io") time.sleep(5) print("io耗时操作完成") res = "io res" callback(res) t1 = Thread(target=func, args=(cb,)) t1.start()def receive_msg(res): print("获取结果回调结果") print("io 结果:", res)def req_a(): print("开始处理请求a---") long_io(receive_msg) print("离开请求a")def req_b(): print("开始处理请求B") time.sleep(2) print("处理完请求B")def main(): req_a() req_b()if __name__ == ‘__main__‘: main()
相关推荐
farewellpoem 2020-11-09
chunjiekid 2020-08-16
lhtzbj 2020-08-13
learnpy 2020-07-19
kyelu 2020-07-09
小菜鸟的代码世界 2020-06-17
举 2020-06-14
bizercsdn 2020-06-06
usepython 2020-05-31
haokele 2020-05-31
fengling 2020-05-31
maimang00 2020-05-30
yawei 2020-05-30
坚持是一种品质 2020-05-28
laityc 2020-05-27
jling 2020-05-19
cenylon 2020-05-15
YENCSDN 2020-05-14
Yasin 2020-05-07