AsyncServerPool¶
pool ¶
Running one app across many servers: :class:AsyncServerPool.
For bots, CADs and dashboards that many servers use (each server's owner gives your app their server key). One pool holds a client per server, shares one connection pool and, with an app key, one set of rate limits (an app key's limits count across every server it calls), and runs your handlers for every server at once.
Example::
from oxrpy import AsyncServerPool
pool = AsyncServerPool(app_key=os.environ["OXFD_APP_KEY"])
pool.add("guild-1234", key_for_guild_1234)
pool.add("guild-5678", key_for_guild_5678)
@pool.on("Kill")
async def on_kill(server, kill): # handlers get the server's name first
await post_kill(server, kill)
await pool.run_forever(save_dir="positions/")
Needs pip install "oxrpy[async]".
AsyncServerPool ¶
AsyncServerPool(*, app_key=None, rate_limit='auto', timeout=10, max_retries=3, base_url=BASE_URL, client=None)
Many servers, one app. See the module docs for an example.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_key
|
Optional[str]
|
Your app key ( |
None
|
rate_limit
|
Union[str, float, None]
|
|
'auto'
|
timeout
|
float
|
Seconds to wait for a response. |
10
|
max_retries
|
int
|
Retries after a 429, or a network error / 500 on a read. |
3
|
base_url
|
str
|
Override the API base URL (for testing). |
BASE_URL
|
client
|
Any
|
Your own |
None
|
add ¶
Add a server under a name you choose (a guild ID, a database ID…). Returns its client, which you can use for that server's reads and commands. If the pool is running, the new server starts being followed straight away.
each
async
¶
Run action(api) for every server at once. Returns {name: result}, where a
server that failed has its exception instead of a result (one failure doesn't stop the
others). Example: await pool.each(lambda api: api.get_players()).
run_command_on_all
async
¶
Run one command on every server (e.g. an announcement). Returns {name: result}.
on ¶
Register a handler, like :meth:OxfordAPI.on, except handlers are called with the
server's name first: async def on_kill(server, kill). error handlers get
(server, exception).
run_forever
async
¶
Follow every server and call the handlers until :meth:stop (or cancellation).
Each server is polled on its own heartbeat. A server whose key stops working (the owner
regenerated it, or the API package lapsed) is dropped from polling, reported to your
error handlers, and listed in :attr:failed; the others carry on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sections
|
Sequence[str]
|
Other sections to fetch every time, for |
()
|
limit
|
Optional[int]
|
Most entries per log per request. |
None
|
skip_existing
|
bool
|
Don't replay what was already in the logs at start (default). |
True
|
save_dir
|
Union[str, Path, None]
|
A folder to keep each server's position in (one small JSON file per server), so a restart carries on where it left off. |
None
|