-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix parallel progress updates and improve naming
- Loading branch information
1 parent
ffa6e4c
commit e6fbe93
Showing
6 changed files
with
17 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,7 @@ | ||
import json | ||
import queue | ||
|
||
|
||
def format_sse(data: str, event=None) -> str: | ||
msg = f"data: {json.dumps(data)}\n\n" | ||
if event is not None: | ||
msg = f"event: {event}\n{msg}" | ||
return msg | ||
|
||
|
||
class MessageAnnouncer: | ||
def __init__(self): | ||
self.listeners = [] | ||
|
||
def listen(self): | ||
q = queue.Queue(maxsize=5) | ||
self.listeners.append(q) | ||
return q | ||
|
||
def announce(self, msg, event=None): | ||
msg = format_sse(msg, event) | ||
for i in reversed(range(len(self.listeners))): | ||
try: | ||
self.listeners[i].put_nowait(msg) | ||
except queue.Full: | ||
del self.listeners[i] | ||
|
||
|
||
announcer = MessageAnnouncer() | ||
return msg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters