Starting & Stopping
Waiting for the connection
Section titled “Waiting for the connection”createService returns a ServiceApi immediately — the connection is established in
the background. open() is how you wait for it:
const service = createService('1.0.0', handler)
await service.open() // resolves once connectedYou rarely need it when the service is the whole program. You do need it before the first
act(), and before any boot loop that
re-attaches deferred keys.
See Two ways to start below — in Python, which constructor you call is how you say whether you want the instance back.
Two ways to start (Python)
Section titled “Two ways to start (Python)”Service(...) configures the service and then consumes on the calling thread, so it never
returns while the service is up:
from requence.service import Service
Service("1.0.0", lambda ctx: ctx.input) # blocks here foreverThat is the right shape when the service is the whole program. But it means there is no
instance to call methods on — so act() and close(), which are both methods, are out of
reach.
Use Service.start(...) when you need either. It is the same configuration with the consume
loop on a thread of its own, and it returns the instance once connected:
import threadingfrom requence.service import Service
service = Service.start("1.0.0", lambda ctx: ctx.input)# ...connected; `service` is usable here
threading.Event().wait() # keep the main thread alive; see belowService(...) |
Service.start(...) |
|
|---|---|---|
| Returns | never (while up) | once connected |
act() / close() |
unreachable | usable |
| Bad credentials | raises | raises |
| Consume thread | the caller’s | a daemon thread |
start(..., timeout=30.0) bounds only the first connect; every drop after it reconnects in
the background as always. A first connect that never lands raises TimeoutError and stops
retrying, so you never get back an instance you cannot use.
Disconnecting
Section titled “Disconnecting”const service = createService('1.0.0', handler)
// Later — gracefully disconnectawait service.close()close() is a method, so it needs Service.start(...):
service = Service.start("1.0.0", handler)
# ...later, from anywhere:service.close()It stops consuming and stops reconnecting, and is safe to call from any thread: the channel work is marshalled onto the connection’s own thread.