Python Forum
get data from 2 async functions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: get data from 2 async functions (/thread-34983.html)



get data from 2 async functions - korenron - Sep-22-2021

hello,
I'm running 2 simple async function (getTime and get random rumber)
the functions are working without any problems

I want to be able to read the current data using websocket.

I have try to run websocket client using java\python\nodejs - in all of them I gete the same problem
get the same answer\data over and over :
22-09-21 11-39-33-175034! this is not 6
22-09-21 11-39-33-175034! this is not 6
22-09-21 11-39-33-175034! this is not 6
22-09-21 11-39-33-175034! this is not 6
22-09-21 11-39-33-175034! this is not 6
22-09-21 11-39-33-175034! this is not 6
22-09-21 11-39-33-175034! this is not 6
22-09-21 11-39-33-175034! this is not 6
and this is the full code:
import asyncio
import websockets
from datetime import datetime
import random


async def connect(websocket, path):
    while True:
        remote_ip = websocket.remote_address
        print('Got request ' + str(remote_ip))
        if RN % 6 == 0:
            await websocket.send(TS + "! this is 6")
        else:
            await websocket.send(TS + "! this is not 6")


async def time_update():
    while True:
        global TS
        TS = datetime.now().strftime('%d-%m-%y %H-%M-%S-%f')
        print(TS)
        # time.sleep(1.8)
        await asyncio.sleep(0.942)


async def randomNumber():
    while True:
        global RN
        RN = random.randrange(0, 10000, 1)
        print(RN)
        await asyncio.sleep(0.215)


asyncio.get_event_loop().create_task(time_update())  
asyncio.get_event_loop().create_task(randomNumber())  

start_server = websockets.serve(hello, "10.0.0.111", 8765)
asyncio.get_event_loop().run_until_complete(start_server) 
print("server started")
asyncio.get_event_loop().run_forever()
this is what the server side is running and printing:
Got request ('10.0.0.111', 65376)
.
.
.
.
Got request ('10.0.0.111', 65376)
22-09-21 11-39-35-889439
22-09-21 11-39-36-841041
22-09-21 11-39-37-792642
what am I doing wrong?

Thanks,