Ethereum: Python ThreadpoolexeCutor closes connection before the task end
Using the Threadpoolexecutor Class Python is very important to control the connections to avoid problems with the task. In this article, we will examine the usual question that may arise from the historical cryptocurrency data from the API duel and keep it in the Postgresql database using Threadpoolexecutor.
Background: Why is the connection closed
The Threadpoolexecutor class creates a separate topic of each task that can cause premature connections if they are not properly controlled. Yes is:
- Each task (such as the resulting historical price data) can establish a connection with API -Jam binance.
- Thread responsible for closing a relationship when it is done with his task may appear before all the tasks are completed.
Problem: Closing a relationship before doing task
In your scenario, you are trying to scan historical cryptocurrency data and keep it in the Postgresql database using Threadpoolexecutor as follows:
`Python
Import configuration
From Binance.Client Customer Import
Imported Psycopg2
Def reach_data ():
Binance API request
Customer = Customer (config.binance_api_key, config.binance_api_secret)
Answer = Customer.get_order_Book ()
Protect the data in the Postgresql database
Conn = psycopg2connect (
host = config.db_host,
user = config.db_user,
Password = config.db_password,
database = config.db_name
)
Cur = conn.cursor ()
In response to response [‘suggestions’]:
Cur.execute (“inserted into historical_data (symbol, price) value ( %s, %s)”, (element [“ID”], element [‘price’]))))))))))))))))
Conn.commit ()
Conn.close ()
Def Main ():
Topics = []
_ Range (config.num_threads):
Nit = nit (target = fetch_data)
nits.Append (thread)
nit.start ()
If __name__ == ‘__main__’:
basic ()
`
In this example, the Futth_data feature creates a connection to the Postgresql database and stores data. However, Threadpoolexeccutor creates more data on historic prices from the API duel. When each thread completes its relationship by completing the task, the other thread tries to do another task without waiting for the previous one to be done.
Solution: Using Concurent.futurus
To solve this problem, you can use the Concurent.futures module, which provides a high -level interface for the asynchronous call. Here's the updated code clip:
Python
Import configuration
From Binance.Client Customer Import
Imported Psycopg2
from at one time.Ufuturens Implloyxexexetor
Def reach_data ():
Binance API request
Customer = Customer (config.binance_api_key, config.binance_api_secret)
Answer = Customer.get_order_Book ()
Protect the data in the Postgresql database
Conn = psycopg2connect (
host = config.db_host,
user = config.db_user,
Password = config.db_password,
database = config.db_name
)
Cur = conn.cursor ()
In response to response [‘suggestions’]:
Try:
Cur.execute (“inserted into historical_data (symbol, price) value ( %s, %s)”, (element [“ID”], element [‘price’])))))))))))))))))
In psycopg2eror as e:
Print (f “was a mistake: {e})
Conn.commit ()
Conn.close ()
Def Main ():
With ThreadpoolexeCutor (max_Workers = config.num_threads) as an executor:
Executor.Map (“reach_data”, range (config.num nits)))))))))))))))
If __name__ == ‘__main__’:
basic ()
`
In this updated code clip, we use Threadpoolexecutor to control the threads and perform tasks asinchron. The Reach_data feature is called 0 d Config.num thread arrow range – 1 “. This ensures that each thread will process its tasks at the same time without waiting for others to end.
Conclusion
Using “at the same time.