Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在同步代码中与sleekxmpp一起使用XMPP?_Python_Asynchronous_Xmpp - Fatal编程技术网

Python 如何在同步代码中与sleekxmpp一起使用XMPP?

Python 如何在同步代码中与sleekxmpp一起使用XMPP?,python,asynchronous,xmpp,Python,Asynchronous,Xmpp,我试图找到一种在同步函数调用中使用异步XMPP库的方法 我想编写一个简单的Python函数,通过XMPP消息发送其参数,接收回复,然后返回此回复: def read_ans(a,b,c): # send [a,b,c] via an XMPP message # wait for XMPP message with reply, z return z 我找到了sleekxmpp库,并为此编写了一个简单的脚本。我遇到的问题是,sleekxmpp在多个线程中运行,并为接收到

我试图找到一种在同步函数调用中使用异步XMPP库的方法

我想编写一个简单的Python函数,通过XMPP消息发送其参数,接收回复,然后返回此回复:

def read_ans(a,b,c):
    # send [a,b,c] via an XMPP message
    # wait for XMPP message with reply, z
    return z
我找到了
sleekxmpp
库,并为此编写了一个简单的脚本。我遇到的问题是,
sleekxmpp
在多个线程中运行,并为接收到的消息指定回调。我不知道如何同步运行它

到目前为止,我所了解的情况如下。如何将
self.last_message
传递到
read_ans
以便返回

谢谢

import sleekxmpp
class XMPP(sleekxmpp.ClientXMPP):

    def __init__(self, jid, password, myargs):
        sleekxmpp.ClientXMPP.__init__(self, jid, password)
        self.add_event_handler("session_start", self.start)

        # Store arguments
        self.myargs = myargs

        # This is where we store the last message received
        self.last_message = ""
        self.add_event_handler("message", self.message)

    def start(self, event):
        self.send_presence()
        self.get_roster()

        self.send_message(mto=recipient, mbody=str(self.myargs))

    def message(self, msg):
        if msg['type'] in ('chat', 'normal'):
            #Store the received message
            self.last_message = msg['body']
            self.disconnect()

def read_ans(a,b,c):
    xmpp = XMPP("user", "pass", [a,b,c])            
    if xmpp.connect():
        xmpp.process(block=True)

    # How can I return the message here?
    return '!?!?'

if __name__ == '__main__':
    read_ans(1,2,3)