Low Level APIs#
This page is an API reference for some of the more low-level APIs contained within TwitchIO.
Disclaimer: Some of these are unstable APIs, meaning that they have no version guarantees, and can break at any time. Those APIs will be marked as such, and are only here for reference purposes.
If you are using these APIs, it is expected that you are proficient in Python, and can teach yourself complex topics.
Shard Managers#
This is just an API reference. For a detailed explanation, please see Sharding.
- class twitchio.BaseShardManager(**kwargs)#
This is an advanced subclassable object that can be passed to your
ClientorBot.New in version 3.0.
- initial_channels#
The initial channels passed to the
ClientorBot. Callables passed to theClient/Botwill have been transformed into a list of channels by this point. If you haven’t passed any initial channels, this will beNone.- Type
list[
str]
- add_shard(shard_id: str, authorized_user: str | None, *, initial_channels: Optional[Iterable[str]] = None) → Shard#
Adds a shard to the manager. This method does not start the shard, to do that, use
Shard.start()- Parameters
- Returns
The shard instance that was created.
- Return type
Shard
- coroutine assign_shard(channel_name: str, is_initial_channel: bool) → None#
This function is a coroutine.
Method to be overriden in a subclass.
This needs to be implemented to assign a channel to a shard (otherwise channels cannot be joined). This will be called in-between
setup()andstart().Note
The channel should be assigned inside of this callback.
- coroutine get_sender_shard(channel_name: str) → Shard#
This function is a coroutine.
Method to be overriden in a subclass.
This method is called whenever the library needs to know what shard to use to send a message with. Unless you have different users logged in on different shards, you can typically return any shard.
Note
You do not need to have joined a channel’s chat to send messages to it.
- Parameters
channel_name (
str) – The channel to send a message to.
- coroutine setup(**kwargs) → None#
This function is a coroutine.
Method to be overriden in a subclass.
This is a setup hook that should be used to create shards, but not start them.
- coroutine start() → None#
This function is a coroutine.
Method to be overriden in a subclass.
This method is called when
Client/Bot.startis called. It should be used to start shards, and should wait until they’ve all exited before returning. You can useawait self.wait_until_exitto do this easily.
- coroutine start_all_shards() → None#
This function is a coroutine.
A helper method to start all shards that have not yet been started.
- coroutine stop() → None#
This function is a coroutine.
Method to be overriden in a subclass.
This method is called when
Client/Bot.stopis called.
- class twitchio.DefaultShardManager(**kwargs)#
The default shard manager. This will be used by default if no other shard manager is passed to
Client.The documentation for each overriden function below will contain it’s implementation for reference purposes.
New in version 3.0.
- main_shard#
The sole shard in use.
- Type
Shard
- coroutine assign_shard(channel_name: str, is_initial_channel: bool) → None#
This function is a coroutine.
Assigns a channel to a shard. Because this implementation only uses one shard, it is quite simple.
The implementation looks like this:
if is_initial_channel: return await self.main_shard.add_channels((channel_name,))
- coroutine get_sender_shard(channel_name: str) → Shard#
This function is a coroutine.
Returns the only shard available. Nothing interesting.
- Parameters
channel_name (
str) – The channel to send a message to.
- coroutine setup(**kwargs) → None#
This function is a coroutine.
Sets up the
DefaultShardManager. CallsTokenHandler.get_irc_token(None)to get a token to log in with.The implementation looks like this:
_, user = await self.token_handler._client_get_irc_login(self.client, None) self.main_shard = self.add_shard("main-shard", user.name)
- class twitchio.DistributedShardManager(**kwargs)#
A distributed shard manager. This shard manager will distribute channels across multiple shards, allowing for more channels to be read from concurrently. This shard manager still uses one user as the login user.
The documentation for each overriden function can be viewed on GitHub.
New in version 3.0.
The following parameters are passed to
Client:- Parameters
channels_per_shard (
int) – The maximum amount of channels per shard. Defaults to 25.max_shard_count (
int|None) – The maxiumum amount of shards that the manager is allowed to create. Defaults to 5.initial_shard_count (
int) – The amount of initial shards that the manager should create and distribute initial channels across. Defaults to 1.
- max_shard_count#
The maximum amount of shards that the manager is allowed to create.
- Type
int|None
- coroutine assign_shard(channel_name: str, is_initial_channel: bool) → None#
This function is a coroutine.
Assigns a channel to a shard. To distribute evenly, this method checks all existing shard levels, and finds the one with the least amount of shards. If all the shards are at their limits, it will create a new shard instead.
The implementation looks like this:
if is_initial_channel: return ideal_shard = min(self.shards.values(), key=lambda shard: len(shard.websocket._channels)) if len(ideal_shard.websocket._channels) >= self.channels_per_shard: if len(self.shards) >= self.max_shard_count: raise RuntimeError("All shards are full, and the shard count limit has been reached.") new_shard_id = f"extended-shard-{self.next_shard_id}" self.next_shard_id += 1 ideal_shard = self.add_shard(new_shard_id, self.authorized_name) await ideal_shard.start(block=False) await ideal_shard.add_channels((channel_name,))
- coroutine get_sender_shard(channel_name: str) → Shard#
This function is a coroutine.
Fetches the first shard available to send from.
The implementation looks like this:
return next(iter(self.shards.values()))
- coroutine setup(**kwargs) → None#
This function is a coroutine.
Sets up the initial state of the shard manager. creates
initial_shard_countshards, and splits the initial channels (if any) into them. If more initial channels are provided than the initial shard count can handle, it will create more.The implementation of this is quite long, please view it on GitHub.