在 python socketio RuntimeWarning 中:协程 'initial' 从未被等待,尽管写了一个 await 语句
In python socketio RuntimeWarning: coroutine 'initial' was never awaited despite writing an await statement
python3 client.py
/home/aijax/.local/lib/python3.6/site-packages/socketio/client.py:592: RuntimeWarning: 协程 'initial' 从未等待
self._handle_event(pkt.namespace, pkt.id, pkt.data)
连接已建立
尽管有等待我得到错误
PS:我对 python 的 Async-io 几乎一无所知我有点为了学校项目在一夜之间完成了这项任务
我的 client.py
import socketio
import time
sio = socketio.Client()
val={"num":35}
@sio.event
def connect():
print('connection established')
@sio.event
def disconnect():
print('disconnected from server')
@sio.event
async def initial(sid):
global val,sio
print("hey there!! ")
await sio.emit('clientUpdate',val)
@sio.event
async def serverAggregate(avg):
global val
val["num"] = (avg["num"] + val["num"])/2
# print(val)
print("kaada ",avg)
time.sleep(4)
await sio.emit('clientUpdate',val)
# await
sio.connect('http://0.0.0.0:8080')
# sio.wait()
# sio.my_message()
sio.wait()
我的任务是创建一个服务器和许多客户端,这些客户端向服务器发送一个数字,然后服务器聚合并将其发送回客户端并执行本地更新并发回数字
我的 server.py
from aiohttp import web
import socketio
import time
import threading
# creates a new Async Socket IO Server
sio = socketio.AsyncServer()
users_count=0 #num of clients
temp_count=users_count
avg={"num":0}
temp_sum=0
initFlag=True
# Creates a new Aiohttp Web Application
# app = web.Application()
async def handle(request):
print(request)
name = request.match_info['name']
text = "Hello, " + name
return web.Response(text=text)
app = web.Application()
sio.attach(app)
async def resetter():
global avg,temp_sum,users_count,temp_count,sio
avg = temp_sum/users_count
temp_count = users_count
temp_sum = 0
print("broadcast requests to clients")
time.sleep(2)
await sio.emit('serverAggregate',avg,broadcast = True, include_self = False)
@sio.on('clientUpdate')
async def serverAggregate(sid, data):
global temp_count,users_count,initFlag,sio,resetter
if initFlag:
temp_count=users_count
initFlag=False
temp_count=temp_count -1
if(temp_count==0):
await resetter()
else:
print("message ", data)
global temp_sum
temp_sum = temp_sum + data["num"]
print("message ", data)
@sio.event
async def connect(sid, environ):
global users_count,sio
print("connect ", sid)
users_count=users_count+1
# time.sleep(3)
# print("say something")
time.sleep(2)
await sio.emit('initial')
# await sio.emit('intialize', broadcast = True, include_self = False)
@sio.event
def disconnect(sid):
global users_count
print('disconnect ', sid)
users_count=users_count-1
app.add_routes([web.get('/', handle),
web.get('/{name}', handle)])
# app.router.add_static('/static', 'static')
class BgProc(threading.Thread):
def __init__(self, sio):
# calling superclass init
threading.Thread.__init__(self)
# self.text = text
self.sio = sio
def run(self):
time.sleep(5)
print("sending broadcast to all clients ")
self.sio.emit("initial",broadcast = True, include_self = False)
if __name__ == '__main__':
web.run_app(app)
您正在使用 socketio.Client()
class,这是标准的 Python 客户端。如果你想写一个asyncio应用,你必须使用socketio.AsyncClient()
.
python3 client.py /home/aijax/.local/lib/python3.6/site-packages/socketio/client.py:592: RuntimeWarning: 协程 'initial' 从未等待 self._handle_event(pkt.namespace, pkt.id, pkt.data) 连接已建立 尽管有等待我得到错误 PS:我对 python 的 Async-io 几乎一无所知我有点为了学校项目在一夜之间完成了这项任务 我的 client.py
import socketio
import time
sio = socketio.Client()
val={"num":35}
@sio.event
def connect():
print('connection established')
@sio.event
def disconnect():
print('disconnected from server')
@sio.event
async def initial(sid):
global val,sio
print("hey there!! ")
await sio.emit('clientUpdate',val)
@sio.event
async def serverAggregate(avg):
global val
val["num"] = (avg["num"] + val["num"])/2
# print(val)
print("kaada ",avg)
time.sleep(4)
await sio.emit('clientUpdate',val)
# await
sio.connect('http://0.0.0.0:8080')
# sio.wait()
# sio.my_message()
sio.wait()
我的任务是创建一个服务器和许多客户端,这些客户端向服务器发送一个数字,然后服务器聚合并将其发送回客户端并执行本地更新并发回数字 我的 server.py
from aiohttp import web
import socketio
import time
import threading
# creates a new Async Socket IO Server
sio = socketio.AsyncServer()
users_count=0 #num of clients
temp_count=users_count
avg={"num":0}
temp_sum=0
initFlag=True
# Creates a new Aiohttp Web Application
# app = web.Application()
async def handle(request):
print(request)
name = request.match_info['name']
text = "Hello, " + name
return web.Response(text=text)
app = web.Application()
sio.attach(app)
async def resetter():
global avg,temp_sum,users_count,temp_count,sio
avg = temp_sum/users_count
temp_count = users_count
temp_sum = 0
print("broadcast requests to clients")
time.sleep(2)
await sio.emit('serverAggregate',avg,broadcast = True, include_self = False)
@sio.on('clientUpdate')
async def serverAggregate(sid, data):
global temp_count,users_count,initFlag,sio,resetter
if initFlag:
temp_count=users_count
initFlag=False
temp_count=temp_count -1
if(temp_count==0):
await resetter()
else:
print("message ", data)
global temp_sum
temp_sum = temp_sum + data["num"]
print("message ", data)
@sio.event
async def connect(sid, environ):
global users_count,sio
print("connect ", sid)
users_count=users_count+1
# time.sleep(3)
# print("say something")
time.sleep(2)
await sio.emit('initial')
# await sio.emit('intialize', broadcast = True, include_self = False)
@sio.event
def disconnect(sid):
global users_count
print('disconnect ', sid)
users_count=users_count-1
app.add_routes([web.get('/', handle),
web.get('/{name}', handle)])
# app.router.add_static('/static', 'static')
class BgProc(threading.Thread):
def __init__(self, sio):
# calling superclass init
threading.Thread.__init__(self)
# self.text = text
self.sio = sio
def run(self):
time.sleep(5)
print("sending broadcast to all clients ")
self.sio.emit("initial",broadcast = True, include_self = False)
if __name__ == '__main__':
web.run_app(app)
您正在使用 socketio.Client()
class,这是标准的 Python 客户端。如果你想写一个asyncio应用,你必须使用socketio.AsyncClient()
.