-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Event Stream. (#122)
* Add support for Event Stream. Co-authored-by: Jonathan Cross <>
- Loading branch information
Showing
8 changed files
with
335 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
## Event | ||
|
||
## Event Stream | ||
|
||
This will setup an event stream. To avoid blocking and having more control to the user it will return a | ||
tuple of (threading.Thread, threading.Event and queue.Queue). You can use your own `queue.Queue` if you want | ||
to use LIFO or SimpleQueue or simply extend upon that. | ||
|
||
### Default | ||
Will listen to all topics | ||
|
||
``` | ||
import nomad | ||
n = nomad.Nomad() | ||
stream, stream_exit_event, events = n.event.stream.get_stream() | ||
stream.start() | ||
while True: | ||
event = events.get() | ||
print(event) | ||
events.task_done() | ||
``` | ||
|
||
### Set Index, Namespace and Topic(s) of Interest | ||
|
||
``` | ||
import nomad | ||
n = nomad.Nomad() | ||
stream, stream_exit_event, events = n.event.stream.get_stream(index=0, topic={"Node": "*"}, namespace="not-default") | ||
stream.start() | ||
while True: | ||
event = events.get() | ||
print(event) | ||
events.task_done() | ||
``` | ||
|
||
### Cancel thread/Optimistically exit | ||
We will use the `stream_exit_event` to get the thread to return/exit gracefully. This isn't immediate | ||
as we have to wait for an event or set an arbitrary timeout value to close/open the connection again. | ||
|
||
In this example we will set `stream_exit_event` right before the timeout, knowing that it needs to re-establish | ||
the connection to the stream. Using a try/except with queue.Queue.get(timeout=<VALUE>) we will check if the thread | ||
is still alive; if it isn't we break the loop. | ||
|
||
``` | ||
import nomad | ||
import threading | ||
import time | ||
import queue | ||
def stop_stream(exit_event, timeout): | ||
print("start sleep") | ||
time.sleep(timeout) | ||
print("set exit event") | ||
exit_event.set() | ||
n = nomad.Nomad() | ||
stream, stream_exit_event, events = n.event.stream.get_stream(index=0, topic={"Node": "*"}, timeout=3.2) | ||
stream.start() | ||
stop = threading.Thread(target=stop_stream, args=(stream_exit_event, 3.0)) | ||
stop.start() | ||
while True: | ||
if not stream.is_alive(): | ||
print("not alive") | ||
break | ||
try: | ||
event = events.get(timeout=1.0) | ||
print(event) | ||
events.task_done() | ||
except queue.Empty: | ||
continue | ||
``` |
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,24 +1,25 @@ | ||
import nomad.api.exceptions | ||
from nomad.api.base import Requester | ||
from nomad.api.jobs import Jobs | ||
from nomad.api.job import Job | ||
from nomad.api.nodes import Nodes | ||
from nomad.api.node import Node | ||
from nomad.api.acl import Acl | ||
from nomad.api.agent import Agent | ||
from nomad.api.allocations import Allocations | ||
from nomad.api.allocation import Allocation | ||
from nomad.api.evaluations import Evaluations | ||
from nomad.api.evaluation import Evaluation | ||
from nomad.api.allocations import Allocations | ||
from nomad.api.base import Requester | ||
from nomad.api.client import Client | ||
from nomad.api.deployment import Deployment | ||
from nomad.api.deployments import Deployments | ||
from nomad.api.evaluation import Evaluation | ||
from nomad.api.evaluations import Evaluations | ||
from nomad.api.event import Event | ||
from nomad.api.job import Job | ||
from nomad.api.jobs import Jobs | ||
from nomad.api.metrics import Metrics | ||
from nomad.api.namespace import Namespace | ||
from nomad.api.namespaces import Namespaces | ||
from nomad.api.node import Node | ||
from nomad.api.nodes import Nodes | ||
from nomad.api.operator import Operator | ||
from nomad.api.regions import Regions | ||
from nomad.api.sentinel import Sentinel | ||
from nomad.api.status import Status | ||
from nomad.api.system import System | ||
from nomad.api.operator import Operator | ||
from nomad.api.validate import Validate | ||
from nomad.api.deployments import Deployments | ||
from nomad.api.deployment import Deployment | ||
from nomad.api.namespaces import Namespaces | ||
from nomad.api.namespace import Namespace | ||
from nomad.api.acl import Acl | ||
from nomad.api.sentinel import Sentinel | ||
from nomad.api.metrics import Metrics |
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
Oops, something went wrong.