platform_driver.interfaces.chargepoint.async_service module

This module is used to process asynchronous requests and cache results for later use. It was written to handle Web API calls to the Chargepoint service but could be used for any long-ish running, gevent friendly function calls.

A single queue is managed by the web_service() function. Requests are placed on the queue by client code, usually with a call to CPRequest.request(). The web_service() function records the request in a dictionary using the method signature (name + parameters). This dictionary maintains a set of AsyncResult objects, one for each request with the same signature.

After recording the request in the dictionary, the web_service() executes the request method in a short-lived greenlet (web_call()). The response is placed on the queue. When the web_service() encounters a response, it sets the values of all the AsyncResults waiting on that request, causing the client greenlets to ‘wake-up’ on an AsyncResult.wait().

The request and response is left in the dictionary until a configurable expiration time so that subsequent requests with the same signature can use the cached result if it has not expired. In this case, the AsyncResult is set immediately.

class platform_driver.interfaces.chargepoint.async_service.CPRequest(method, timeout, *args, **kwargs)[source]

Bases: object

Encapsulates a method to be called asynchronously.

The result is returned as AsyncResult. The request() classmethod is used to create a request, queue it to the web_service() and return an AsyncResult that the caller can wait() on.

is_request()[source]
key()[source]
classmethod request(method, timeout, *args, **kwargs)[source]

Generate a new request and put it on the web service queue.

Returns the requests AsyncResult instance which will be filled in after the request has been executed.

result()[source]
property timeout
class platform_driver.interfaces.chargepoint.async_service.CPResponse(key, response, client)[source]

Bases: object

A response to to a CPRequest invocation.

property client
is_request()[source]
key()[source]
response()[source]
class platform_driver.interfaces.chargepoint.async_service.CacheItem(cache_life)[source]

Bases: object

A cached request/response.

As responses come in, they are matched to the originating request and waiting_results are ‘set’. Subsequent requests with the same signature (key) are satisfied immediately, if not expired, by setting the async result on the incoming request.

property expiration
property request
property response
property waiting_results
platform_driver.interfaces.chargepoint.async_service.web_call(request, client)[source]

Wraps the request to be executed.

This is spawned as a greenlet and puts the request result on the queue.

platform_driver.interfaces.chargepoint.async_service.web_service()[source]

Cache/service request loop.

Reads items from the web_service_queue. It is intended to be spawned as a greenlet

that runs forever.

If the de-queued item is a CPRequest, the cache is checked for an existing response. If not found, the request is added to cache and a greenlet is spawned to complete the request.

If the de-queued item is a CPResponse, the item is found in cache and all waiting AsyncResults are set with the response. The response will stay in cache until expiration.