sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
6.06k stars 2.21k forks source link

requests.exceptions.ReadTimeout #88

Closed echel0n closed 6 years ago

echel0n commented 6 years ago
Traceback (most recent call last):
  File "trader.py", line 631, in <module>
    main()
  File "trader.py", line 621, in main
    bt.action(client.get_order_book(symbol=option.market.upper() + option.funding.upper()))
  File "trader.py", line 489, in action
    status = client.get_order(symbol=market + funding, orderId=order['orderId'])['status']
  File "/usr/local/lib/python2.7/dist-packages/binance/client.py", line 1116, in get_order
    return self._get('order', True, data=params)
  File "/usr/local/lib/python2.7/dist-packages/binance/client.py", line 180, in _get
    return self._request_api('get', path, signed, version, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/binance/client.py", line 154, in _request_api
    return self._request(method, uri, signed, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/binance/client.py", line 148, in _request
    response = getattr(self.session, method)(uri, timeout=10, **kwargs)
  File "/root/.local/lib/python2.7/site-packages/requests/sessions.py", line 521, in get
    return self.request('GET', url, **kwargs)
  File "/root/.local/lib/python2.7/site-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/root/.local/lib/python2.7/site-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/root/.local/lib/python2.7/site-packages/requests/adapters.py", line 521, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.binance.com', port=443): Read timed out. (read timeout=10)
echel0n commented 6 years ago

I have get_order_book function running in a 2 second loop and this error randomly happens and breaks the loop

sammchardy commented 6 years ago

Hi @echel0n, a timeout parameter of 10s was added to the requests calls recently to avoid .

If the response is taking longer than that amount of time you would see this error, so indicates that either Binance takes a long time to respond, or the network connection to the Binance server is slow, or a combination of these factors.

You can catch this timeout exceptions along with ConnectTimeout exceptions using the requests.exceptions.Timeout exception class.

I could also make this timeout value a parameter when creating the binance.client class if that would help your situation.

echel0n commented 6 years ago

I could see the benefit of making it a param for me and others as I've always found timeouts with requests lib to vary depending on the connection being used to make the request.

I should be able to use the param combined with some extra code to catch the timeout and perform a retry then give up if unsuccessful after X number of attempts, each retry I can increase the timeout this way as well.

Thanks!

sammchardy commented 6 years ago

Have included an option in v0.6.0 to override requests parameters, globally or on a call by call basis.

See the documentation here https://python-binance.readthedocs.io/en/latest/overview.html#requests-settings