| title | icon |
|---|---|
Python |
python |
import MvpWarning from "/snippets/mvp-warning.mdx"; import StepDefineActor from "/snippets/step-define-actor.mdx"; import StepRunStudio from "/snippets/step-run-studio.mdx"; import StepDeploy from "/snippets/step-deploy.mdx"; import SetupNextSteps from "/snippets/setup-next-steps.mdx";
The RivetKit Python client provides a way to connect to and interact with actors from Python applications.
Create a new directory for your project:```sh
mkdir my-app
cd my-app
```
It's recommended to create a virtual environment:
```sh
python -m venv venv
source venv/bin/activate # On Windows use: venv\Scripts\activate
```
```sh
pip install rivetkit-client
```
<CodeGroup>
```python Async
import asyncio
from rivetkit_client import AsyncClient
async def main():
# Replace with your endpoint URL after deployment
client = AsyncClient("http://localhost:6420")
# Get or create a actor instance
counter = await client.get("counter")
# Subscribe to events using callback
def on_new_count(msg):
print(f"Event: {msg}")
counter.on_event("newCount", on_new_count)
# Call an action
result = await counter.action("increment", 5)
print(f"Action result: {result}")
# Wait to receive events
await asyncio.sleep(1)
# Clean up
await counter.disconnect()
if __name__ == "__main__":
asyncio.run(main())
```
```python Sync
from rivetkit_client import Client
# Replace with your endpoint URL after deployment
client = Client("http://localhost:6420")
# Get or create a actor instance
counter = client.get("counter")
# Subscribe to events using callback
def on_new_count(msg):
print(f"Event: {msg}")
# Clean up once we receive our event
counter.disconnect()
counter.on_event("newCount", on_new_count)
# Call an action
result = counter.action("increment", 5)
print(f"Action result: {result}")
# Clean is handled on by on_new_count
```
</CodeGroup>
In the code above, subscription is done with `on_event` callbacks, but you can also
subscribe directly with `receive()` calls, using the `SimpleClient` (and `AsyncSimpleClient`)
interfaces. See our [sample usage](https://github.com/rivet-gg/rivetkit/tree/main/clients/python/tests/test_e2e_simple_async.py) for more details.
```sh
python main.py
```
You should see output like:
```
Event: 5
Action result: 5
```
Run it again to see the state update.