Python线程错误:此事件循环已在运行
如果您希望运行代码,请确保执行Python线程错误:此事件循环已在运行,python,multithreading,python-multithreading,python-3.6,discord,Python,Multithreading,Python Multithreading,Python 3.6,Discord,如果您希望运行代码,请确保执行pip安装discord.py 我有以下三个python文件,它们一起工作以连接到discord服务器 Launch.py from Sender import * from Reciever import * class Launcher: def startThreads(sendOB, recOB, username): threads = [] recThread = threading.Thread(target
pip安装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()
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()
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')
receiver.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()
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()
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')
Sender.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()
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()
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使用线程运行sender和receiver,但当我运行Launch.py时,会出现此异常
在thread-1:Traceback(most)中输入用户名:异常
最近调用(最后):文件“C:\Program
文件\Python36\lib\site packages\discord\client.py”,第519行,正在运行
self.loop.run_直到_完成(self.start(*args,**kwargs))文件“C:\Program Files\Python36\lib\asyncio\base_events.py”,第454行,在
运行直到完成
self.run\u forever()文件“C:\Program Files\Python36\lib\asyncio\base\u events.py”,第408行,在run\u forever中
raise RUNTIMERROR(“此事件循环已在运行”)RUNTIMERROR:此事件循环已在运行
在处理上述异常期间,发生了另一个异常:
回溯(最近一次调用):文件“C:\Program”
Files\Python36\lib\threading.py”,第916行,在\u bootstrap\u内部
self.run()文件“C:\Program Files\Python36\lib\threading.py”,第864行,在run中
self.\u target(*self.\u args,**self.\u kwargs)文件“C:\Users\user\Desktop\Discord\HopeForBetter\receiver.py”,第26行,
大体上
client.run('mzaznjuyodywnjy1anti4mzix.C9bPOA.4ISE_jmY1BYlPq937zGpjISuvAI')
文件“C:\Program Files\Python36\lib\site packages\discord\client.py”,
第534行,运行中
self.loop.close()文件“C:\Program Files\Python36\lib\asyncio\selector\u events.py”,第107行,关闭
raise RUNTIMERROR(“无法关闭正在运行的事件循环”)RUNTIMERROR:无法关闭正在运行的事件循环
我能想到的唯一一件事是,由于两个线程都在访问同一个文件,所以它导致了一个异常,但这似乎不够合理,但我也是线程方面的新手。我在一个从DB读取、不访问任何文件的脚本上遇到了同样的问题。你找到解决办法了吗?