Python 中的异步调用转为同步调用

发布于 2024-10-19 06:33:08 字数 510 浏览 0 评论 0原文

本质上,我将调用远程 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街角卖回忆 2024-10-26 06:33:08

你的主张纯属无稽之谈。 xmlrpclib 操作是同步且阻塞的。为了执行异步操作等,您需要使用线程来实现一些东西。

Your claim is nonsense. xmlrpclib operations are synchronous and blocking. For performing asynchrous operations etc. you need to implement something using threads.

亢潮 2024-10-26 06:33:08

我同意 pynator 的观点...

但是如果您需要有关阻止部分的帮助...阻止的设计非常简单:

class GUI():
    def __init__(self):
        self.blocking_thread = Thread(target=self.get_data)
        self.client = xmlrpclib.ServerProxy(...)

    def  query_for_data():
         self.blocking_thread.start()
         self.blocking_thread.join()

    def get_data(self):
        while(True):
            #this assumes this returns some how and doesn't block..
            result = self.client.add(...)
            if(result): break;

            time.sleep(1)

I agree with pynator...

But in case you need help on the blocking piece...the design to block is pretty simple:

class GUI():
    def __init__(self):
        self.blocking_thread = Thread(target=self.get_data)
        self.client = xmlrpclib.ServerProxy(...)

    def  query_for_data():
         self.blocking_thread.start()
         self.blocking_thread.join()

    def get_data(self):
        while(True):
            #this assumes this returns some how and doesn't block..
            result = self.client.add(...)
            if(result): break;

            time.sleep(1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文