Добрый вечер.
Я пытаюсь сделать бот на сокетах, который при получении на сокет, будет отправлять её в спец. канал.
Вот выделил под получатель свой поток, а в этом потоке уже запускается asyncio.run(асинхронная функция), вот код:
untchatsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
untchatsock.connect(("127.0.0.1", 4250))
async def sendmsgTX(_rxpkg):
nchannel = client.get_channel(755448398584479824)
modchannel = client.get_channel(720254558248501329)
rxpkg = _rxpkg.decode('utf-8')
rxmassiv = rxpkg.split('|')
if rxmassiv[0] == 1:
await nchannel.send(":untgo: " + rxmassiv[1] + " Connected !")
await modchannel.send(":untgo: " + rxmassiv[1] + " Connected ! " + '(' + rxmassiv[2] + ')')
elif rxmassiv[0] == 2:
await nchannel.send(":untback: " + rxmassiv[1] + " Disconnected ")
await modchannel.send(":untback: " + rxmassiv[1] + " Disconnected ! " + '(' + rxmassiv[2] + ')')
else:
await nchannel.send('['+rxmassiv[1]+']: ' + rxmassiv[2])
def UntChatRX():
while True:
_rxpkg = untchatsock.recv(1024)
nchannel = client.get_channel(755448398584479824)
asyncio.run(sendmsgTX(_rxpkg))
#loop = asyncio.new_event_loop()
#asyncio.set_event_loop(loop)
#loop.run_until_complete(sendmsgTX(_rxpkg))
thread = threading.Thread(target = UntChatRX)
thread.start()
Но мне выдает ошибку:
Traceback (most recent call last):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "bot.py", line 577, in UntChatRX
asyncio.run(sendmsgTX(_rxpkg))
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\asyncio\runners.py", line 43, in run
return loop.run_until_complete(main)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "bot.py", line 570, in sendmsgTX
await nchannel.send('['+rxmassiv[1]+']: ' + rxmassiv[2])
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\abc.py", line 890, in send
data = await state.http.send_message(channel.id, content, tts=tts, embed=embed,
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\http.py", line 185, in request
async with self.__session.request(method, url, **kwargs) as r:
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiohttp\client.py", line 1012, in __aenter__
self._resp = await self._coro
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiohttp\client.py", line 426, in _request
with timer:
File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\aiohttp\helpers.py", line 579, in __enter__
raise RuntimeError('Timeout context manager should be used '
RuntimeError: Timeout context manager should be used inside a task