Python Forum
get data from 2 async functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get data from 2 async functions
#1
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,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked async was never was never awaited but the await is still in the code Nietzsche 3 2,003 Jun-04-2024, 02:28 PM
Last Post: deanhystad
  Async requests lukee 0 2,223 Oct-06-2020, 04:40 AM
Last Post: lukee
  Taking serial data to perform key.press functions ausbollinger13 1 3,668 Sep-04-2020, 10:26 PM
Last Post: bowlofred
  Async IO writing to two Different Tables Help TiagoV 0 3,734 Oct-09-2019, 04:45 AM
Last Post: TiagoV
  Issues with async and yielding from API GSerum 1 3,074 Dec-18-2018, 08:37 PM
Last Post: nilamo
  async question on raspberry pi baukeplugge 2 100,765 Nov-07-2018, 07:58 PM
Last Post: baukeplugge
  module functions and data references Skaperen 6 6,153 Jul-30-2017, 04:09 AM
Last Post: Skaperen
  Async server/client Bokka 2 5,318 May-29-2017, 03:09 PM
Last Post: Bokka

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.