python多线程实现异步
2021-02-20 09:19
标签:__name__ 结果 请求 back 操作 target 获取 tar sleep python多线程实现异步 标签:__name__ 结果 请求 back 操作 target 获取 tar sleep 原文地址:https://www.cnblogs.com/guo-s/p/12682714.htmlimport time
from threading import Thread
def 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()