Asynchronous Support — gntplib 0.5 documentation

# Asynchronous Support

`gntplib.async` provides support for asynchronous processing built on Tornado.

## Publisher

`AsyncPublisher` provides asynchronous methods: `register()` and `publish()`. These methods are inherited from `Publisher` directly.

Note that they are asynchronous methods especially when you use them in a row. `register()` returns regardless of wheather the `REGISTER` request has been received by the GNTP server, so successive `publish()` method call is possible to fail due to unregistered application or notification name.

To avoid this, pass `publish()` method call to `callback` keyword argument as callback function:

    >>> publisher = AsyncPublisher('App', ['Async Event'])
    >>> def my_callback(ignored):
    ...     publisher.publish('Async Event', 'Title')
    >>> publisher.register(callback=my_callback)

Or use Tornado’s `gen.Task` syntax like this:

    >>> @gen.engine
    ... def async_publish():
    ...     publisher = AsyncPublisher('App', ['Async Event'])
    ...     yield gen.Task(publisher.register)
    ...     publisher.publish('Async Event', 'Title')

## Subscriber

`AsyncSubscriber` provides asynchronous method `subscribe()`, which is inherited from `Subscriber` directly.

## Resource

`AsyncResource` is a lazy resource. Before request dispatching, the resource data is fetched asynchronously from the url passed to the constructor.

You can use `AsyncResource` instead of `Resource` in `AsyncPublisher` or `AsyncSubscriber`:

    >>> icon = AsyncResource('http://example.org/icon.png')
    >>> resource = AsyncResource('http://example.org/resource.pdf')
    >>> publisher = AsyncPublisher('App', ['Async Event'],
    ...                            custom_headers=[('resource', resource)])
    >>> publisher.publish('Async Event', 'Title', icon=icon)