Python 中的异步调用转为同步调用
本质上,我将调用远程 XMLRPC 服务器,它将异步处理请求。
import xmlrpclib
client = xmlrpclib.ServerProxy('http://localhost:8080')
client.add(3,5)
def add_result(result):
print result
我知道在将来的某个时候, add_result 将会被调用并显示结果。问题是。我希望能够将调用 client.add 转换为将返回结果的阻塞调用。我这样做是为了一个将调用我的 GUI。问题是我应该在哪里阅读有关此类解决方案的信息?我不太确定从哪里开始。
我认为我根本没有很好地解释自己。 我调用的服务器正在实现异步部分。当我调用 add 时,它将返回 true。我知道服务器希望我实现 add_result ,这就是它对我的调用。我想做的就是清理这个疯狂的计划,以便有人可以对我调用 add,并且我会阻塞,直到对我调用 add_result,然后我将返回给给我打电话的人。我希望这能解决问题
In essence I am going to make a call to a remote XMLRPC server and it will process the request asynchronously.
import xmlrpclib
client = xmlrpclib.ServerProxy('http://localhost:8080')
client.add(3,5)
def add_result(result):
print result
I know at some point in the future that add_result will get called with the result. The thing is. I want to be able to turn the call client.add into a blocking call that will return the result. I'm doing this for a GUI that will be calling on me. The question is where should I be looking to read about this sort of solution? I'm not really sure where to start.
I don't think I've explained myself well at all.
The server I am calling is implementing the aynchronous part. When I call add it will return true. And I know that the server is expecting me to implement add_result which is what it will call on me. What I am trying to do is clean this crazy scheme up so that someone can call add on me and I will block until add_result is called on me, I will then return to whoever called me. I hope this clears things up
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的主张纯属无稽之谈。 xmlrpclib 操作是同步且阻塞的。为了执行异步操作等,您需要使用线程来实现一些东西。
Your claim is nonsense. xmlrpclib operations are synchronous and blocking. For performing asynchrous operations etc. you need to implement something using threads.
我同意 pynator 的观点...
但是如果您需要有关阻止部分的帮助...阻止的设计非常简单:
I agree with pynator...
But in case you need help on the blocking piece...the design to block is pretty simple: