Search
 
SCRIPT & CODE EXAMPLE
 

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))
Comment

PREVIOUS NEXT
Code Example
Python :: python exe restart 
Python :: Redirect to same page after POST method using class based views 
Python :: pandas from multiindex to single index 
Python :: # print random number 
Python :: get dataframe deminsions 
Python :: cornell hotel sustainability benchmarking index 
Python :: how call a class in another class python 
Python :: xgb plot importance 
Python :: Count the data points based on columns 
Python :: to create an array with values that are spaced linearly in a specified interval 
Python :: print numbers 1 to 10 using recursion in python 
Python :: fill missing values with dict 
Python :: Convert Int to String Using string formatting 
Python :: Math Module acos() Function in python 
Python :: cubic interpolation python 
Python :: Using **kwargs to pass the variable keyword arguments to the function 
Python :: jdoodle python 
Python :: how to plot a single centroid 
Python :: cartopy indicate lat lon 
Python :: find not in dafatrame series 
Python :: Python NumPy Shape function example Printing the shape of the multidimensional array 
Python :: use count() function to find if a row is there in sqlite database or not. 
Python :: Python NumPy stack Function Syntax 
Python :: Python NumPy append Function Example Working with axis 
Python :: python __sub__ 
Python :: Program to get number of consecutive repeated substring 
Python :: NumPy invert Code When inputs are Boolean 
Python :: change admin password djano 
Python :: green book résumé 
Python :: python to dart converter 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =