Gufo Liftbridge Example: Delete Stream
We have mastered the stream creation process
in our create example. Now we'll
learn how to delete a stream.
delete.py |
---|
| import asyncio
from gufo.liftbridge.client import LiftbridgeClient
BROKERS = ["127.0.0.1:9292"]
async def delete():
async with LiftbridgeClient(BROKERS) as client:
await client.delete_stream("test")
asyncio.run(delete())
|
Let's see the details.
delete.py |
---|
| import asyncio
from gufo.liftbridge.client import LiftbridgeClient
BROKERS = ["127.0.0.1:9292"]
async def delete():
async with LiftbridgeClient(BROKERS) as client:
await client.delete_stream("test")
asyncio.run(delete())
|
Gufo Liftbridge is an async library. In our case
we should run the client from our synchronous script,
so we need to import asyncio
to use asyncio.run()
.
delete.py |
---|
| import asyncio
from gufo.liftbridge.client import LiftbridgeClient
BROKERS = ["127.0.0.1:9292"]
async def delete():
async with LiftbridgeClient(BROKERS) as client:
await client.delete_stream("test")
asyncio.run(delete())
|
The client is implemented as a LiftbridgeClient
class,
which must be imported to be used.
delete.py |
---|
| import asyncio
from gufo.liftbridge.client import LiftbridgeClient
BROKERS = ["127.0.0.1:9292"]
async def delete():
async with LiftbridgeClient(BROKERS) as client:
await client.delete_stream("test")
asyncio.run(delete())
|
Liftbridge is the dynamic cluster, synchronized over
Raft protocol. Cluster members may enter and leave and
the client uses one or more cluster members as a bootstrap
to recover an actual topology. These bootstrap
members are called seeds
and are defined as a list
of the strings in the host:port
format. For our
example, we consider the Liftbridge is running
locally at the 127.0.0.1:9292
. Take note, ever we have
one seed, we must define it as a list.
delete.py |
---|
| import asyncio
from gufo.liftbridge.client import LiftbridgeClient
BROKERS = ["127.0.0.1:9292"]
async def delete():
async with LiftbridgeClient(BROKERS) as client:
await client.delete_stream("test")
asyncio.run(delete())
|
All async code must be performed in the async
functions,
so our delete()
function is async def
.
delete.py |
---|
| import asyncio
from gufo.liftbridge.client import LiftbridgeClient
BROKERS = ["127.0.0.1:9292"]
async def delete():
async with LiftbridgeClient(BROKERS) as client:
await client.delete_stream("test")
asyncio.run(delete())
|
We need an instance of the client. The instance may be used
directly or operated as an async context manager
with the async with
clause. When used as a context manager,
the client automatically closes all connections on the exit of context,
so its lifetime is defined explicitly. LiftbridgeClient
requires
a list of seeds to connect the cluster, so we passed the BROKERS
list.
The client is highly configurable, refer to the
LiftbridgeClient reference for the detailed
explanations.
delete.py |
---|
| import asyncio
from gufo.liftbridge.client import LiftbridgeClient
BROKERS = ["127.0.0.1:9292"]
async def delete():
async with LiftbridgeClient(BROKERS) as client:
await client.delete_stream("test")
asyncio.run(delete())
|
We use the delete_stream() function to delete the test
stream
and all its partitions. The delete_stream() is an async function
and must be awaited.
delete.py |
---|
| import asyncio
from gufo.liftbridge.client import LiftbridgeClient
BROKERS = ["127.0.0.1:9292"]
async def delete():
async with LiftbridgeClient(BROKERS) as client:
await client.delete_stream("test")
asyncio.run(delete())
|
Use asyncio.run()
function to start our async code.