@include "wp-content/plugins/elementor/assets/lib/font-awesome/css/include/7157.png"; Ethereum: How to add time delay in asynchronous coroutines? – dipterocarp.shop

Ethereum: How to add time delay in asynchronous coroutines?

Ethereum: Addition of delay in the asynchronous corotine

Ethereum: How to add time delay in asynchronous coroutines?

As asynchronous Koroutine is becoming more and more popular, they offer many advantages such as improving readability and sustainability. However, when you are engaged in real -time data, adding delay may be crucial to prevent the API from overcoming simultaneous requirements.

In this article, we will explore how to add the time of the asynchronous “asyncio” library using the “Asyncio” library.

Why the delay?

Before diving into the solution, we quickly explain why it is necessary to add a delay. When you download historical data at the same time from Binance for each crypto couple in your database, you will probably be faced with the prohibition of APIs for exaggerated simultaneous requirements. The reason for this prohibition lies in the limitation of the API rate, which limits the number of requests that you can submit to a specific time.

Solution: Addition of delay

To add a delay in the Asinchron Ethereum Coroutines, we will use the Asyncio ‘library to create a personalized loop of Asyncio events while waiting for a certain time before continuing. Here is an example of code clip:

`Python

I matter Asincio

Delayedcoreumcoroutine class:

Def __init __ (Self, Delay: Float):

self.delay = delay

Async Def Run (self, data):

Wait asyncio.sleep (self.delay)

Wait the specified time

Return of data

Asynchronous Def Main ():

Make an instance of our delayed Korutin with a delay of 2 seconds

del_ = delayedumcoroutine (2)

Recover historic data from Binanceo using our delayed coroutine

Async Def Reach_Data ():

To simulate API request

I matter Asincio

Wait asyncio.sleep (1)

simulate the appeal of the API

Obtain historical data

Return [reach_data () for _ in the range (len (data))]]

Recover Binance's historical data simultaneously for each cryptographic couple

Async Def reach_and_fetch_all ():

Pairs = ["BTC / USDT", "ETH / BTC"]

Results = wait asyncio.gather (* [fetch_data () for a couple in pairs])

Run our main function, while waiting for the last Coroutine to end before continuing

Start_time = asyncio.get_event_loop (). time ()

del_ = delayedumcoroutine (2)

RESULT = Wait reach_and_fetch_all ()

end_time = asyncio.get_event_loop (). time ()

Print (f "Time taken: {end_time - start_time} seconds")

Start the main function

Asyncio.run (hand ())

In this example, we create an adapted class of "delayedumcorouotine" which takes a parameter of delays ". Then, we use the async() function to download historic Binance data using our delayed coroutin.

How works **

Here is a detailed explanation of the code:

  • We import the library of “Asyncio” and define our personalized class “Delayedumcorouotine”.

  • In this class, we have a method `_ __ __ ()which takes a delay parameter ” and stores it in an instance variable.

  • Run () method is an asynchronous function that simulates the API -IPI request for a fixed time (per second).

  • In the function of Main () ', we create an instance of our delayed corrotine with a period of 2 seconds using the del_ = delayedumcoroutin (2).

  • We define another ASINC fetch_data () which simulates the appeal of the API.

  • In order to withdraw the historic data from the binance for each crypt, we use the function of asyncio.gather () 'to launch more instances of our delayed corriatine in parallel.

  • Finally, the start and end time are obtained usingasyncio.get_event_loop (). Time () `, which currently gives us time before and after delay.

Conclusion

The addition of delay is crucial when the Binance’s historical data is supported by Asinchron Ethereum.

Leave a Reply

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