Python线程错误:此事件循环已在运行

时间:2017-04-19 01:43:05

标签: python multithreading python-multithreading python-3.6 discord

如果您希望运行代码...请务必执行pip install discord.py

我有以下三个python文件,它们一起工作以连接到discord服务器

Launch.py​​

from Sender import *
from Reciever import *

class Launcher:

    def startThreads(sendOB, recOB, username):
        threads = []
        recThread = threading.Thread(target=recOB.main, args=())
        sendThread = threading.Thread(target=sendOB.main, args=(username))
        threads.append(sendThread)
        threads.append(recThread)
        for i in range(2):
            threads[i].start()
        for i in range(2):
            threads[i].join()
    def launcherMain(self):
        username = input("Enter your username: ")
        theSender = Send()
        theReciever = Recieve()
        Launcher.startThreads(theSender, theReciever, username)

launchOB = Launcher()
launchOB.launcherMain()

Reciever.py

import discord, threading, asyncio

client = discord.Client()

class Recieve:
    index = None

    @client.event
    async def on_message(message):
        #Run through decrypter here
        if message.author == client.user:
            return
        Recieve.index = message.content.find(": ")
        print(str(Recieve.index))
        #print(message.content[0:Recieve.index] + "~-#" + message.content[Recieve.index + 2:len(message.content))



    @client.event
    async def on_ready():
        print("Reciever Ready to go")
        print('---------')


    def main(self):
        client.run('TokenErasedForSecurity')

#rec = Recieve()
#rec.main()

Sender.py

import discord, threading, asyncio
client = discord.Client()


class Send:
    username = ""
    async def messageSender():
        channel = discord.Object(id='IdErasedForSecurity')
        while True:
            messageToSend = Send.username + ": "
            messageToSend += input(Send.username + '~->')
            await client.send_message(channel, messageToSend)


    @client.event
    async def on_ready():
        print("Sender Ready to go")
        print('---------')
        client.loop.create_task(Send.messageSender())

    def main(self, theUsername):
        Send.username = theUsername
        client.run('TokenErasedForSecurity')

Launch.py​​使用线程运行发送方和接收方,但是当我运行launch.py​​时,我得到了这个异常

  

输入您的用户名:线程中的异常Thread-1:Traceback(大多数   最近的呼叫最后):文件" C:\ Program   文件\ Python36 \ lib \ site-packages \ discord \ client.py",第519行,在运行中       self.loop.run_until_complete(self.start(* args,** kwargs))File" C:\ Program Files \ Python36 \ lib \ asyncio \ base_events.py",第454行,in   run_until_complete       self.run_forever()文件" C:\ Program Files \ Python36 \ lib \ asyncio \ base_events.py",第408行,在run_forever中       引发RuntimeError('此事件循环已在运行')RuntimeError:此事件循环已在运行

     

在处理上述异常期间,发生了另一个异常:   回溯(最近一次调用最后一次):文件" C:\ Program   在_bootstrap_inner中的Files \ Python36 \ lib \ threading.py",第916行       self.run()文件" C:\ Program Files \ Python36 \ lib \ threading.py",第864行,在运行中       self._target(* self._args,** self._kwargs)文件" C:\ Users \ user \ Desktop \ Discord \ HopeForBetter \ Reciever.py",第26行,   在主要       client.run(' MzAzNjUyODYwNjY1NTI4MzIx.C9bPOA.4ISE_jmY1BYlPq937zGpjISuvAI')   文件" C:\ Program Files \ Python36 \ lib \ site-packages \ discord \ client.py",   第534行,在运行中       self.loop.close()文件" C:\ Program Files \ Python36 \ lib \ asyncio \ _ selector_events.py",第107行,关闭       引发RuntimeError("无法关闭正在运行的事件循环")RuntimeError:无法关闭正在运行的事件循环

我唯一能够想到的是它导致异常,因为两个线程都访问同一个文件,但这看起来似乎不太合理,但我也是线程新手。 / p>

0 个答案:

没有答案