Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to run 2 async function forever

import websockets
import asyncio
import json
import time

async def another_name():
    while True:
        millis = int(round(time.time() * 1000))
        print(millis)
        await asyncio.sleep(0.001)

async def stream():
    async with websockets.connect('wss://www.bitmex.com/realtime?subscribe=trade:XBTUSD') 
                    as websocket:
        while True:
            try:
                data = await websocket.recv()
                data = json.loads(data)
                print(data['data'][-1]['price'])
            except KeyError:
                pass
            except TypeError:
                pass
            await asyncio.sleep(0.001) #Added

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    coros = []
    coros.append(another_name())
    coros.append(stream())
    loop.run_until_complete(asyncio.gather(*coros))
Source by www.stackfinder.ru #
 
PREVIOUS NEXT
Tagged: #run #async #function
ADD COMMENT
Topic
Name
9+4 =