Python 3中的XMPP线程接收器

时间:2019-05-27 06:26:16

标签: python multithreading python-asyncio sleekxmpp

我想做的是使用Slixmpp库设置XMPP接收器,该库在脚本执行过程中侦听消息。我认为线程库可能提供了一个解决方案,因此尝试了一下。 该脚本用于在线PvP游戏,我需要不断从对手那里接收信息。

这是我尝试过的代码:

import time
import threading as td
import slixmpp as slix
import logging
import asyncio

my_jid = "test@xmpp.jp"
my_pass = "test"

class receiver(slix.ClientXMPP):
    def __init__(self, jid, password):
        slix.ClientXMPP.__init__(self, jid, password)
        self.add_event_handler("session_start", self.start)
        self.add_event_handler("message", self.message)

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

    def message(self, msg):
        if msg['type'] in ('chat', 'normal'):
            msg.reply("Thanks for sending\n%(body)s" % msg).send()
            print(msg['body'])

def function():
    recv = receiver(my_jid, my_pass)
    recv.connect()
    recv.process()



newthread = threading.Thread(target=function)
logging.basicConfig(level="DEBUG", format='%(levelname)-8s %(message)s')


newthread.start()

input("Press Enter to continue")

这将返回以下错误:

Exception in thread Thread-1:
Press Enter to continueTraceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Program Files (x86)\Python37-32\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/dps/Documents/GitHub/Python-Problems/UltimateTicTacToe-scripts/xmppp.py", line 46, in good
    recv = receiver(my_jid, my_pass)
  File "C:/Users/dps/Documents/GitHub/Python-Problems/UltimateTicTacToe-scripts/xmppp.py", line 32, in __init__
    slix.ClientXMPP.__init__(self, jid, password)
  File "C:\Users\dps\venv\lib\site-packages\slixmpp\clientxmpp.py", line 70, in __init__
    BaseXMPP.__init__(self, jid, 'jabber:client', **kwargs)
  File "C:\Users\dps\venv\lib\site-packages\slixmpp\basexmpp.py", line 48, in __init__
    XMLStream.__init__(self, **kwargs)
  File "C:\Users\dps\venv\lib\site-packages\slixmpp\xmlstream\xmlstream.py", line 219, in __init__
    self.disconnected = asyncio.Future()
  File "C:\Program Files (x86)\Python37-32\lib\asyncio\events.py", line 644, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-1'.

0 个答案:

没有答案