Consumer implementation
Project description
requence
This package connects python to the operator bus. It manages the retrieval and responses of messages.
Usage
from requence.consumer import ContextHelper, Consumer
def handleMessage(context: ContextHelper):
return "this is my response"
Consumer({}, handleMessage)
Every consumer instance needs a url parameter to connect to the operator and a version. In the basic example, those parameters get automatically retrieved from environment variables REQUENCE_URL and VERSION.
To be more explicit about those configurations, you can pass an object as the first parameter:
Consumer(
{
'url': 'your operator connection url string',
'version': 'your version in format major.minor.patch',
},
handleMessage
)
Additional configuration
By default, the consumer retrieves one message from the operator, processes it and passes the response back to the operator. If your service is capable of processing multiple messages in parallel, you can define a higher prefetch.
Consumer(
{
'prefetch': 10, # this will process max. 10 messages at once when available
},
handleMessage
)
Unsubscribing service
Should you ever need to unsubscribe your service from the operator programmatically, Consumer has a close method.
consumer = Consumer(...)
# later
consumer.close()
To resubscribe, you have to instantiate a new Consumer
Processing messages
The message handler callback provides one argument: the message context. The context provides helper methods to access previous operator results and also methods to abort the processing early.
context api data retrieval
ctx.get_input(): Optional[Any]
The input that was defined when the task started
ctx.get_meta(): Optional[Any]
The additional meta information added to the task
ctx.get_configuration(): Optional[Any]
The optional configuration of the current service
ctx.get_service_meta(serviceIdentifier: str): TypedDict{
'executionDate': Optional[datetime], # null when the service was not yet executed
'id': str, // service id used for internal routing
'alias': Optional[str], # service alias (see service Identifier)
'name': str,
'configuration': Optional[Any],
'version': str
}
The meta parameters of a service, usually not needed
ctx.get_service_data(serviceIdentifier: str): Any
The response of a previously executed service
ctx.get_last_service_data(serviceIdentifier: str): Any
Same as ctx.get_service_data but only returns the last data when a service is used multiple times
ctx.get_service_error(serviceIdentifier: str): str | None
The error message of a previously executed service or null when said service was not yet executed or did process without error.
ctx.get_last_service_error(serviceIdentifier: str): str | None
Same as ctx.get_service_error but only returns the last error when a service is used multiple times
ctx.get_results(): Array[TypedDict{
'executionDate': Optional[datetime], # null when the service was not yet executed
'id': str, # service id used for internal routing
'alias': Optional[str], # service alias (see service Identifier)
'name': str,
'configuration': Optional[Any],
'version': str
'error': Optional[str]
'data': Optional[Any]
}]
get results of all configured services in this task. When a service did run prior to the current service, executionDate and error or data will be available.
context api processing control
ctx.retry(delay: Optional[int]): None
Instructs the operator to retry the service after an optional delay in milliseconds (minimum is 100ms). No code gets executed after this line. Currently, it is your responsibility to prevent infinite loops.
ctx.abort(reason: Optional[str]): None
Instructs the operator to abort the processing of this service immediately. If the service is not configured with a fail over, the complete task will fail.
service identifier
Most context methods require a service identifier as parameter. This identifier can either be a service name or a service alias. The latter is useful for situations where a service is used multiple times in a task and needs the data from one specific execution.
Full example
Pseudo implementation of a database service
from requence.consumer import ContextHelper, Consumer
from someDb import db
def handleMessage(context: ContextHelper):
ocrData = ctx.get_service_data("ocr")
if (not ocrData):
ctx.abort("Ocr service is mandatory for this AI service")
if (not db.isConnected):
# lets wait 2s for the db to recover
ctx.retry(2000)
response = db.getDataBasedOnOcr(ocrData)
return response
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file requence-0.1.2.tar.gz.
File metadata
- Download URL: requence-0.1.2.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
307707178faca9e3955188ba222418abe893828e4f74a0b44303f718c9e1d3bf
|
|
| MD5 |
34b4b7aff784997c04975771b02cdfa3
|
|
| BLAKE2b-256 |
ec2301d6ff851da77063957d4568aaf1a352c8281253f7f62720e4df0ff802f9
|
File details
Details for the file requence-0.1.2-py3-none-any.whl.
File metadata
- Download URL: requence-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab7021ca11e9b39621cc893855e4ffb79135879329afc3a13a6580e8a7f941c2
|
|
| MD5 |
6ebe7aa380eac41007c1425c77e27f49
|
|
| BLAKE2b-256 |
05f1ea62f549da695b7ea21a2bffa1c9054adaba35687a71a4bf704b55ad85cc
|