betfair: stream.stop() breaks when async=True
When calling stream.stop()
when async=True
, I get the following error:
Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run() File "/usr/lib/python2.7/threading.py", line 754, in run self.__target(*self.__args, **self.__kwargs) File "/usr/local/lib/python2.7/dist-packages/betfairlightweight/streaming/betfairstream.py", line 191, in _read_loop received_data_raw = self._receive_all() File "/usr/local/lib/python2.7/dist-packages/betfairlightweight/streaming/betfairstream.py", line 223, in _receive_all raise SocketError('Connection closed by server') SocketError: Connection closed by server
This is because the threads calling stream._read_loop
don’t have time to see that stream._running
has been set to False
, thus are still trying to read from the socket, even though the socket has been closed by the stream.stop()
.
A dirty solution to this is to do:
stream._running = False
time.sleep(5) # this gives time for _read_loop threads to see they need to stop reading from the socket.
stream.stop()
However, I think I nicer solution would to stop the threads directly in the stream.stop() function before the socket is closed.
I can add this fix and test locally. Would you want me to merge this into master?
Cheers, Jack
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 19 (10 by maintainers)
Commits related to this issue
- closes #165 running handling added to empty string to prevent error being raised mid recv — committed to betcode-org/betfair by liampauling 5 years ago
- Merge pull request #212 from liampauling/bugfix/165-stop-socket-error closes #165 — committed to betcode-org/betfair by liampauling 5 years ago
Realizing that I’m the one who worked to get the package running originally on Python 2.7, maybe it’s worth it to try to figure out how many people are on either side of the 2/3 split. While I thought I was going to be using it with 2.7, I ended up eventually converting everything I needed to Python 3.6.
It’s reasonable to consider dropping 2.7 support at some point especially if it simplifies things are not that many users need the support. I know a lot of scientific/data analytics packages are planning for dropping support for Python 2:
https://python3statement.org/
Feel free to pick this up in a new issue if there is going to be a lengthy discussion.
I have fixed on my local version of the package by joining the threads before closing the socket (see: https://github.com/liampauling/betfair/pull/166)- so it is not an issue for me anymore. I just thought I would share my solution in case anyone else had the same issue. Cheers, Jack