有人可以解释一下如何在python中的异步调用之间传递数据的概念吗?
我有这种情况:
我有一个主过程(mainthread),然后运行另一个异步调用,该调用将两个numbers()相加并休眠一段时间.期望让主线程等到calc和sleeping完成.在伪代码方面,这可能看起来像这样:
def example():
def my_calc(x,y):
z = x+ y
time.sleep(2)
return z #this should get pushed to queue,but for simplicity we use 'return z'
z = asyncSomething.callInThread(my_calc,2,20) #assume we get z from queue
return z+10
def runner(): #our main function
print 'start'
z = example()
print 'result is {0}'.format(z)
如何使最后一次打印等待z?我尝试使用threading.Event()并进行设置,等待和清除,但它们阻止了所有操作.
我认为我的案子不是唯一的,必须有一种整齐的系统方法来处理.我看着扭曲的反应堆,但部分成功.我的实际代码涉及GUI,必须等待异步过程的结果,然后进行自我更新和自动更新…但是我认为该示例描述了大部分问题
我想我错过了如何在python中异步工作的实际概念.
Return the value returned by the call. If the call hasn’t yet completed then this method will wait up to timeout seconds. If the call hasn’t completed in timeout seconds,then a concurrent.futures.TimeoutError will be raised. timeout can be an int or float. If timeout is not specified or None,there is no limit to the wait time.
如上面的注释中所述,如果您不使用多线程(并且使用异步编程),那么回调将是解决之道.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。