Async API Reference¶
Asynchronous API using Tornado framework.
Publisher & Subscriber¶
AsyncPublisher¶
- class AsyncPublisher(name, event_defs, icon=None, io_loop=None, **kwargs)[source]¶
Bases:
PublisherAsynchronous GNTP notification publisher.
Same as Publisher but uses AsyncGNTPClient for non-blocking operations. Requires Tornado.
Example
>>> import asyncio >>> from gntplib.async_gntp import AsyncPublisher >>> from gntplib import Event >>> >>> async def main(): ... pub = AsyncPublisher('MyApp', [Event('test')]) ... pub.register() ... pub.publish('test', 'Title', 'Message') >>> >>> asyncio.run(main())
- __init__(name, event_defs, icon=None, io_loop=None, **kwargs)[source]¶
Initialize async publisher.
- Parameters:
- Raises:
ImportError – If Tornado is not installed
AsyncSubscriber¶
- class AsyncSubscriber(id_, name, hub, password, port=23053, io_loop=None, **kwargs)[source]¶
Bases:
SubscriberAsynchronous GNTP notification subscriber.
Same as Subscriber but uses AsyncGNTPClient for non-blocking operations. Requires Tornado.
Example
>>> import asyncio >>> from gntplib.async_gntp import AsyncSubscriber >>> >>> async def main(): ... sub = AsyncSubscriber('id', 'name', 'hub.example.com', 'pass') ... sub.subscribe() >>> >>> asyncio.run(main())
- __init__(id_, name, hub, password, port=23053, io_loop=None, **kwargs)[source]¶
Initialize async subscriber.
- Parameters:
- Raises:
ImportError – If Tornado is not installed
Client & Connection¶
AsyncGNTPClient¶
- class AsyncGNTPClient(io_loop=None, **kwargs)[source]¶
Bases:
GNTPClientAsynchronous GNTP client using Tornado.
Extends GNTPClient with async resource fetching capabilities.
- io_loop¶
Tornado IOLoop instance
- __init__(io_loop=None, **kwargs)[source]¶
Initialize async client.
- Parameters:
io_loop (IOLoop | None) – Tornado IOLoop (default: current loop)
**kwargs – Additional arguments for GNTPClient
- Raises:
ImportError – If Tornado is not installed
AsyncGNTPConnection¶
- class AsyncGNTPConnection(address, timeout, final_callback, socket_callback=None, io_loop=None)[source]¶
Bases:
BaseGNTPConnectionAsynchronous GNTP connection using Tornado IOStream.
Provides non-blocking socket communication with GNTP server.
- stream¶
Tornado IOStream for async socket operations
- __init__(address, timeout, final_callback, socket_callback=None, io_loop=None)[source]¶
Initialize async connection.
- Parameters:
- Raises:
ImportError – If Tornado is not installed
- write_message(message)[source]¶
Send message to server asynchronously.
- Parameters:
message (bytes) – Message bytes to send
Resources¶
AsyncResource¶
- class AsyncResource(url)[source]¶
Bases:
ResourceResource that will be fetched asynchronously from URL.
Use this class when you want to fetch icons or images from remote URLs asynchronously before sending the notification.
- url¶
URL to fetch resource from
- data¶
Resource data (populated after fetch)
Utility Functions¶
- async fetch_async_resources_in_parallel(async_resources)[source]¶
Fetch multiple AsyncResource URLs in parallel.
Uses asyncio.gather to fetch all resources concurrently. Failed fetches are logged but don’t stop the function.
- Parameters:
async_resources (List[AsyncResource]) – List of AsyncResource instances
- Returns:
Same list with data populated
- Return type:
Example
>>> resources = [ ... AsyncResource('https://example.com/icon1.png'), ... AsyncResource('https://example.com/icon2.png') ... ] >>> await fetch_async_resources_in_parallel(resources)
- collect_async_resources(request)[source]¶
Collect all AsyncResource instances from a request.
Traverses the request object to find all AsyncResource instances that need to be fetched before the request can be sent.
- Parameters:
request (Any) – GNTP request object
- Returns:
List of unique AsyncResource instances
- Return type:
Example
>>> from gntplib import Event >>> from gntplib.requests import RegisterRequest >>> icon = AsyncResource('https://example.com/icon.png') >>> request = RegisterRequest('MyApp', icon, [Event('test')]) >>> resources = collect_async_resources(request)