register events to avoid processing duplicate events#7
Conversation
trz42
left a comment
There was a problem hiding this comment.
The change prevents intentional resending of events, e.g., when developing the bot. Maybe it’s good enough as a temporary workaround.
In any case, I would not hardcode the number of threads to one. I think it would be ok to make it a parameter here and for the bot.
pyghee/main.py
Outdated
| log("App started!") | ||
| waitress.serve(app, listen='*:3000') | ||
| # stick to single thread so we can avoid processing duplicate event deliveries multiple times | ||
| waitress.serve(app, listen='*:3000', threads=1) |
There was a problem hiding this comment.
I wouldn’t do that. It could result in the bot becoming unresponsive if the processing of an event takes a long time.
There was a problem hiding this comment.
Short-term, this is a necessary evil, otherwise the detection of duplicate events can't work, that relies on serially processing events...
By default, threads is 4 with waitress, and to be honest I don't expect big impact of this, since processing a single event shouldn't take much longer than a couple of seconds.
There was a problem hiding this comment.
Handling of deploy event may take a while, which could result in some delays in processing other events.
If we obtain a lock in register_event and release it when we're done, forcing a single thread won't be necessary
There was a problem hiding this comment.
Hmm, maybe I'm being too careful here, because of the Python GIL (https://docs.python.org/3/glossary.html#term-global-interpreter-lock)?
So maybe I don't need to restrict this to a single thread at all even though multiple threads can update global state (the self.registered_events list)...
That's true, but I don't see an easy way around that... For a development scenario just restarting the bot would be enough, since the registered events are only kept in memory. |
|
Just chiming in, I've tried running this version of PyGHee on the bot instance at RUG and did a "lame" stress test by sending a bunch of bot commands in quick succession here: Neves-Bot/software-layer#42 I did see waitress warnings in the |
|
@trz42 How shall we proceed here? Merge as is, and check if there's trouble due to the changes (on the |
pyghee/lib.py
Outdated
| if event_id in self.registered_events: | ||
| return False | ||
| else: | ||
| self.registered_events.append(event_id) |
There was a problem hiding this comment.
The locking can be done here according to ChatGPT
...
import threading
...
# Thread lock for safe access
lock = threading.Lock()
...
with lock:
self.registered_events.append(event_id)
There was a problem hiding this comment.
import threading
# Create a lock once and reuse it
lock = threading.Lock()
def safe_function():
with lock: # Reuses the same lock
# Critical section (thread-safe code)
print("Thread-safe operation")
|
Locking added, so no more need to stick to a single-thread. I'll get this merged, so we can battle-test this, but should work... |
requested change made, no longer using single-threaded run
No description provided.