Hi,
I’m working on a chessboard project based on an ESP32 and having an “issue” to manually close the Lichess event stream.
If you’d like to take a look (see LichessWorkerEvent, but you dont have to, I’ll explain the idea and the issue below):
https://github.com/onlytrialanderror/chessboard/blob/main/ha_lichess_adapter/lichess_components.py
The current implementation is based on theberserk library. The logic opens an event stream (in a separate thread) for the user currently selected on the chessboard. This works fine. However, when I select a different user, I want to close the event stream of the previous user and immediately open a new stream for the new user.
The stream only closes when the next event is received. That’s the problem: I have to wait until the next event arrives.
According to the documentation, an “empty” event is sent every 7 seconds, which means I should only have to wait up to 6–7 seconds.
Here is a minimal example:
import berserk
API_TOKEN = 'xxxx'
def main():
session = berserk.TokenSession(API_TOKEN)
_client_lichess_event = berserk.Client(session=session)
count = 0
for event in _client_lichess_event.board.stream_incoming_events():
if event:
print(f"Event: {event}")
print(f'Count: {count}-> {event}')
count += 1
if __name__ == "__main__":
main()
I would expect to see the “Count” message printed every 7 seconds, but I don’t see that happening.
So my questions are:
What is the best way to close the event stream?
Is there a way to avoid waiting for the next event (i.e. skip the 7-second delay)?
Why I dont see the "count"-message every 7 seconds ? (what is my missunderstanding here)
I’ve been thinking about triggering a kind of “dummy” event to force the stream to return, but I can’t find a suitable way to do this. Sending a dummy message to the user might be possible, but it doesn’t feel like a good solution.
Do you have any suggestions?
Thanks!