self.call
doesn't work in Thread?
#1086
-
I'm using Tried googling and got nothing useful. Tried searching for '.call' or 'self.call' on issues and discussions. I'm getting zero result. And honestly, I think this using Not sure if this is a bug or not, so posting on discussion. I've copy-pasted the reproduction code below. I have 3 cases that I tested. The first case is using The second case is using Thread and The third case uses
but the stack is all about aiohttp and asynctio... I would love to use And as a side note, for case 2 and 3, when the server emits or calls, the client receives the message after 20 seconds of delay in my case. Do you know why this is the case and how to fix this? Reproduction code:# server.py ==============
# Python libs
import asyncio, datetime, signal
from threading import Thread
# Pip libs
import socketio
from aiohttp import web
from bson import json_util
# Without this, when I ctrl+c in the server, I get `KeyboardInterrupt` with
# `status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms)` and it won't exit
signal.signal(signal.SIGINT, signal.SIG_DFL)
class AsyncServer(socketio.AsyncNamespace):
def __init__(self):
super().__init__()
self.app = web.Application()
self.sio = socketio.AsyncServer()
self.sid = None
async def on_connect(self, sid, environ):
print("on connect", sid)
self.sid = sid
# CASE 2 -------------------------------
# t = Thread(target=asyncio.run, args=(self.threadTaskEmit(),))
# t.start()
# CASE 3 -------------------------------
# t = Thread(target=asyncio.run, args=(self.threadTaskCall(),))
# t.start()
return
async def on_disconnect(self, sid):
print("on disconnect", sid)
return
# REMOVE THIS AFTER CASE 1 -------------------------------
async def on_connected(self, sid):
print("On connected", datetime.datetime.now())
response = await self.call("request", sid=self.sid)
print("Got response:", type(response))
print(response)
def start(self):
self.sio.register_namespace(self)
self.sio.attach(self.app)
web.run_app(self.app, port=8000)
# For case 2
async def threadTaskEmit(self):
def cb(response):
print("Got response:", type(response))
print(response)
print("Emit request", datetime.datetime.now())
await self.emit("request", callback=cb)
# For case 3
async def threadTaskCall(self):
print("Call request", datetime.datetime.now())
response = await self.call("request", sid=self.sid)
print("Got response:", type(response))
print(response)
if __name__ == "__main__":
AsyncServer().start() The client code: # Python libs
import asyncio, datetime
# Pip libs
import socketio
# 2022-11-13 12:35
# For readability of `ApiNodeBase`
class AsyncClient(socketio.AsyncClientNamespace):
def __init__(self):
super().__init__()
self.sio = socketio.AsyncClient()
return
async def on_connect(self):
print(f"connected")
# REMOVE THIS AFTER CASE 1 -------------------------------
await self.emit("connected")
async def on_disconnect(self):
print(f"disconnected!")
await self.disconnect()
def on_connect_error(self, data):
print("The connection failed!")
def start(self):
self.sio.register_namespace(self)
async def run():
await self.sio.connect('http://localhost:8000')
await self.sio.wait()
asyncio.run(run())
return
def on_request(self):
# response = json.dumps({ "foo": "bar" })
response = { "foo": "bar" }
print("on request:", datetime.datetime.now())
print(response)
return response
# For testing stuff
if __name__ == "__main__":
AsyncClient().start() Edit. Changing The default timeout is 60, but it doesn't work when timeout is not given. It WORKS when I set |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You seem to be missing the fact that asyncio is single threaded. You cannot mix threads with asyncio in this way, things are going to fail in strange ways as this isn't a supported usage. |
Beta Was this translation helpful? Give feedback.
You seem to be missing the fact that asyncio is single threaded. You cannot mix threads with asyncio in this way, things are going to fail in strange ways as this isn't a supported usage.