60262186
Lok Sze Decoration Engineering Limited
Flat C7, 5/F, Tung Lee Factory Building, 9 Lai Yip Street, Kwun Tong, Kowloon

  • 中文 (香港)
  • English
  • Ethereum: python program running functions simultaneously instead of running sequentially

    Using Ethereum Features Simultaneously with Binance Connector and WebSockets

    In this article, we will show you how to create a Python program that performs multiple operations simultaneously using the binance connector library to create orders on the Ethereum blockchain and the “unicorn-binance-websocket-api” application to stream real-time data.

    Prerequisites

    • Install the required libraries:

    pip install binance-connector unicorn-binance-websocket-api

    • Replace YOUR_BNB_API_KEY, YOUR_BNB_API_SECRET, and YOUR_ORDER_ID with your actual Binance API ID and order ID.

    Code

    Ethereum: python program running functions simultaneously instead of running sequentially

    import asyncio

    from binance connector import client

    from unicorn_binance_websocket_api import BinanceWebsocketAPI






    Install Ethereum client

    eb = client()

    eb.load_secret_key_from_file('path/secret.key')


    Configure WebSocket connection

    wsa = BinanceWebsocketAPI(eb, 'BTCUSDT', 'streaming')

    async def create_order():


    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"Order created: {result}")

    async def get_order_status():


    Get the status of the created order by streaming the user data socket

    async for message wsa.get_messages():

    if message ['data']['type'] == 'order':


    Assuming the message contains an "order" object

    print(f"Order ID: {message['data']['id']} - Status: {'successful' if message['data']['state'] == 2 else 'failed'})

    async def main():

    await create_order()

    await get_order_status()

    asyncio.run(main())

    Explanation

    • The “create_order” function uses the “binance-connector” function to create a new order on the Ethereum blockchain.
    • The get_order_status function uses a WebSocket stream to retrieve the status of the created order from the Binance User Data API.
    • In the “Main” function, we first create an order with the “create_order” parameter. Then we execute the “get_order_status” function in an asynchronous loop.

    Running code

    Save this code as a Python file (e.g. “eth_order.py”) and execute it with the command “python eth_order.py”.

    Note: This is just a basic example to show how to perform multiple operations at once. In a real-world situation, you may want to handle errors and exceptions more efficiently.

    Tip

    • Be sure to replace the placeholder values ​​in the code with your actual Binance API ID and order ID.
    • Consider enabling error and exception handling to ensure the stability and reliability of your program.
    • You may need to modify the “get_order_status” function to match the data structure returned by the Binance User Data API.

    PRIVATE SALE TRADING PSYCHOLOGY