Ethereum: Adding time delay in asynchronous coroutins
Because asynchronous corners are becoming more and more popular, they offer many benefits, such as better readability and behavior. However, when searching for data in real time, adding a time delay can be crucial to prevent the API interface preventing with concurrent demands.
In this article, we will examine how to add a time delay in asynchronous Ethereum Coroutines using the “Asyncio” library.
Why a time delay?
Before immersing ourselves in the solution, let’s quickly discuss why it is necessary to add a time delay. When downloading historical data simultaneously with binance for each couple of cryptocurrencies in the database, you probably encounter the API interface bans due to excessive concurrent demands. The reason for this prohibition is to limit the API interface rate, which limits the number of demands that you can submit within a specific time frame.
solution: Adding time delay
To add a time delay in asynchronous Ethereum Coroutines, we will use the “Asyncio” library to create a non -standard ASYNCIO event loop, which awaits a certain amount of time before continuing. Here is an example fragment of the code:
`Python
Import asyncio
The class is delayed, ŻemCoroutine:
Def __init __ (self, delay: float):
self.delay = delay
Async Def Run (Self, data):
Wait for asyncio.sleep (self.delay)
wait for a certain amount of time
Returning data
Async def main ():
Create an instance of our delayed Coroutine with a 2-second delay
Del_ = delayingmeumcoroutine (2)
Recover historical data from Binance with our delayed corridor
async defin fetch_data ():
Simuluj API request
Import asyncio
Wait for asyncio.sleep (1)
simulate the API connection
Download historical data
Return [fetch_data () for _ in the scope (linen (data))]
Recover historical data simultaneously with binance for each cryptographic pair
async defin fetch_and_fetch_all ():
pairs = ["btc/usdt", "eth/btc"]
Results = wait for asyncio.gather (*[fetch_data () for pairs in pairs]))
Start our main function that is waiting for the end of the delayed Coroutine
start_time = asyncio.get_event_loop (). Time ()
Del_ = delayingmeumcoroutine (2)
result = wait for fetch_and_fetch_all ()
end_time = asyncio.get_event_loop (). Time ()
print (f "Time took: {end_time - start_time} seconds")
Start the main function
asyncio.run (main ())
In this example, we create a non -standard class ‘delayingcoroutine’, which adopts the ‘delay’ parameter. Then we use the Async Main () 'function to download historical data from Binance using our delayed Coroutine.
how it works
Here is the explanation of the code step by step:
- We import the "Asyncio" library and define our non -standard class "delayingcoroutine".
- In this class we have the method
__init __ () ', which adopts the "delay" parameter and stores it in a variable instance.
- The "Run () method is an asynchronous function that simulates the request of the API by sleeping for a certain time (in seconds).
- In the “Main ()
function, we create the instance of our delayed Coroutina with a 2-second delay using
del_ = delayingmeumcoroutine (2).
- We define a different function asyncFetch_data ()
Simulates API call.
- To download historical data simultaneously from binance for each couple of cryptocurrencies, we use the “asyncio.gather ()
parallel to the launch of many instances of our delayed Coroutina.
- Finally, the start and final times are obtained using “asyncio.get_event_loop (). Time () `, which gives us the current time before and after delay.
Application
Adding a time delay is crucial when downloading historical data simultaneously with binance in asynchronous Ethereum Coroutines.