PdCom5 Subscriber¶
-
class
pdcom5.
Transmission
(period)¶ Specifies how a variable is transferred to the client.
- Parameters
period (datetime.timedelta) – Periodic transmission.
-
class
pdcom5.
SubscriberBase
(process, transmission: pdcom5._PdComWrapper.Transmission)¶ -
newValues
(autoclose=True) → AsyncGenerator[datetime.timedelta, Any]¶ Entry point for library users to process incoming data.
Changed in version 5.3: Use generator instead of context manager.
This function returns an asynchronous generator. In the body of the
async for
loop, the values of the subscriptions assigned to this subscriber are guaranteed to not change. If you break out of the loop early, all subscriptions are cancelled automatically, unlessautoclose
is set to False.- Parameters
autoclose – Close connections when leaving the loop, defaults to True
The following example shows how to subscribe to two variables with a period of one second.
import pdcom5 process = pdcom5.Process() await process.connect("msr://localhost") subscriber = process.create_subscriber(1.0) cos = await subscriber.subscribe("/osc/cos") sin = await subscriber.subscribe("/osc/sin") async for timestamp in subscriber.newValues(): print(f"At {timestamp}, cos was {cos.value}" + f" and sin was {sin.value}.")
Please do not do any blocking operations in the body of the loop, like sleeping for a long time. The reason is that the library has to cache the incoming values to make sure no data is skipped. Also, using the same subscription in multiple concurrent tasks is not allowed, for the same reason. Just create one subscriber per task.
If you want to reuse existing subscriptions and break out of the loop early, pay attention to close the generator fully before reusing. This can be done using a
try...finally
block or by using contextlib.aclosing.timestamp_gen = subscriber.newValues(autoclose=False) try: async for timestamp in timestamp_gen: process(timestamp) break finally: await timestamp_gen.aclose() # subscriber.newValues() can now be called again
-
-
class
pdcom5.
Subscriber
(process, transmission: pdcom5._PdComWrapper.Transmission)¶ Variable subscriber.
This class manages how variables are subscribed and the callback when new values are received.
- Parameters
process – Process instance
transmission – Kind of subscription (poll, event based, periodic)
-
async
subscribe
(variable: Union[str, pdcom5._PdComWrapper.Variable], selector: Optional[pdcom5._PdComWrapper.Selector] = None) → pdcom5.Subscription.Subscription¶ Subscribe to a variable.
- Parameters
variable – Variable to subscribe.
selector – Optional selector to create a view on multidimensional data.
- Returns
a Subscription instance.