Search code examples
pythonflaskpython-asyncioaiohttppython-socketio

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: coroutine 'initial' was never awaited self._handle_event(pkt.namespace, pkt.id, pkt.data) connection established despite of having the await I'm getting the error PS: I have little to no knowledge of Async-io of python I kinda have finish this task overnight for a school proj my 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()

My task is to create a server and many clients which send a number to server and server aggregates that and sends it back to the client and perform a local update and send back the number my 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)

Solution

  • You are using the socketio.Client() class which is the standard Python client. If you want to write an asyncio application, you must use socketio.AsyncClient().