Ethereum: python program running functions simultaneously instead of running sequentially

Starting Ethereum works simultaneously with Binance-Connector and Websockets

In this article, we demonstrate how to create Python, which at the same time operates several features, using the “Binance-Connector” library to create orders on blockchaine ethereum and “Unicorn-Bine-Websocket-APIfor Real Time Real Time data.

assumptions

Ethereum: python program running functions simultaneously instead of running sequentially

  • Install the required libraries:

Bash

PIP Install Binance-Connector of Unicorn-Binance-Websocket-API

`

  • Replace your_bnb_ap_ap_KEY,your_bnb_ap_Api_secret and your_order_id with your true API binance binance API and order ID.

code

`Python

import asyncio

from the binance connector’s import client

From Unicorn_banance_websocket_api Import BinanceWeBSEKETAPIPIPIPIPIP

Set the Ethereum client

eb = client ()

eb.load_secret_Key_from_file (‘Path/to/secret.Key’)

Set up WebSocket connect

WSA = BinanceWeBSEKETAPIPIPIPIPIPIPIPIPIP (EB, ‘BTCUSDT’, ‘Streaming’)

Async defer defder ():

Create a new order with Binance-Connector

data = {

“Side”: “Buy”,

“Type”: “Limit”,

“Time_in_force”: “GTC”,

“Quantity”: 10,

“Price”: 10000.0

}

Result = AWAIT EB.PLACE_ORDER (DATA)

Print (f “created order: {result}”)

Async defer_order_status ():

Get the status of the created order using the streaming of the User Socket

Async for message in wsa.get_messages ():

If Message [‘Data’] [‘Type’] == ‘Order’:

Assuming an object ‘order’ is stored in a message

Print (f “Order ID: {message [‘data’] [‘ID’]} – status: {‘success’, if message [‘data’] [‘State’] == 2 Else ‘failed’))

Async Def Main ():

Expect Create_order ()

Expect Get_order_status ()

Asyncio.Run (Main ())

`

Explanation

  • Function Create_order usesBinance-Connector" to enter a new Blockchain Ethereum command.

  • FunctionGet_order_statususes the streaming of the WebSocket to get the status of the order created from the Binance User Data API.

  • In the function "Main we first create an order with the” CREATE_ORDER “. Then start the Get_ORDER_STATUS in asynchronous loop.

Launch code

Save this code as a python file (eg eth_order.py) and do it using the python et_order.py.

Note: This is just a basic example that demonstrates how to run multiple functions at the same time. In the real world scenario, you would probably like to handle mistakes and exceptions more robust.

Council

  • Be sure to replace the values ​​of the surrender symbols in the code with your real API credentials of the API Binance API and the order ID.

  • Consider implementing error manipulation and exception management to ensure that your program remains stable and reliable.

  • You may need to editGet_order_status` to match the structure of the Binance API data returned.

PRIVATE PRIVATE TRANSACTIONS

Leave a Reply

Your email address will not be published. Required fields are marked *