|
| 1 | +# Examples |
| 2 | + |
| 3 | +This page shows practical ways to use `rsloop`. |
| 4 | + |
| 5 | +All examples use normal Python `asyncio` patterns. The difference is that `rsloop` provides the event loop implementation. |
| 6 | + |
| 7 | +## Run one coroutine |
| 8 | + |
| 9 | +This is the simplest starting point: |
| 10 | + |
| 11 | +```python |
| 12 | +import rsloop |
| 13 | + |
| 14 | + |
| 15 | +async def main() -> None: |
| 16 | + print("hello from rsloop") |
| 17 | + |
| 18 | + |
| 19 | +rsloop.run(main()) |
| 20 | +``` |
| 21 | + |
| 22 | +Use this style when your program has one main async entry point. |
| 23 | + |
| 24 | +## Create tasks inside the loop |
| 25 | + |
| 26 | +This looks almost the same as standard `asyncio`: |
| 27 | + |
| 28 | +```python |
| 29 | +import asyncio |
| 30 | +import rsloop |
| 31 | + |
| 32 | + |
| 33 | +async def worker(name: str) -> str: |
| 34 | + await asyncio.sleep(0.1) |
| 35 | + return f"{name} done" |
| 36 | + |
| 37 | + |
| 38 | +async def main() -> None: |
| 39 | + task1 = asyncio.create_task(worker("first")) |
| 40 | + task2 = asyncio.create_task(worker("second")) |
| 41 | + results = await asyncio.gather(task1, task2) |
| 42 | + print(results) |
| 43 | + |
| 44 | + |
| 45 | +rsloop.run(main()) |
| 46 | +``` |
| 47 | + |
| 48 | +If your app already uses `asyncio.create_task(...)`, `await`, and `gather(...)`, moving to `rsloop` is usually straightforward. |
| 49 | + |
| 50 | +## Create a loop manually |
| 51 | + |
| 52 | +Manual loop creation is useful when you want explicit setup and cleanup: |
| 53 | + |
| 54 | +```python |
| 55 | +import asyncio |
| 56 | +import rsloop |
| 57 | + |
| 58 | + |
| 59 | +async def main() -> None: |
| 60 | + print("running on", type(asyncio.get_running_loop()).__name__) |
| 61 | + |
| 62 | + |
| 63 | +loop = rsloop.new_event_loop() |
| 64 | +asyncio.set_event_loop(loop) |
| 65 | +try: |
| 66 | + loop.run_until_complete(main()) |
| 67 | +finally: |
| 68 | + asyncio.set_event_loop(None) |
| 69 | + loop.close() |
| 70 | +``` |
| 71 | + |
| 72 | +Use this style if your program manages the loop lifecycle itself. |
| 73 | + |
| 74 | +## Schedule callbacks |
| 75 | + |
| 76 | +`rsloop.Loop` supports the familiar callback APIs: |
| 77 | + |
| 78 | +```python |
| 79 | +import rsloop |
| 80 | + |
| 81 | + |
| 82 | +loop = rsloop.new_event_loop() |
| 83 | + |
| 84 | + |
| 85 | +def say(message: str) -> None: |
| 86 | + print(message) |
| 87 | + loop.stop() |
| 88 | + |
| 89 | + |
| 90 | +loop.call_later(0.5, say, "timer fired") |
| 91 | +loop.run_forever() |
| 92 | +loop.close() |
| 93 | +``` |
| 94 | + |
| 95 | +This is useful when reading older `asyncio` code that still uses callbacks instead of only coroutines. |
| 96 | + |
| 97 | +## Work with raw sockets |
| 98 | + |
| 99 | +You can use socket helpers directly on the running loop: |
| 100 | + |
| 101 | +```python |
| 102 | +import asyncio |
| 103 | +import socket |
| 104 | +import rsloop |
| 105 | + |
| 106 | + |
| 107 | +async def main() -> None: |
| 108 | + loop = asyncio.get_running_loop() |
| 109 | + left, right = socket.socketpair() |
| 110 | + left.setblocking(False) |
| 111 | + right.setblocking(False) |
| 112 | + |
| 113 | + try: |
| 114 | + await loop.sock_sendall(left, b"ping") |
| 115 | + data = await loop.sock_recv(right, 4) |
| 116 | + print(data.decode()) |
| 117 | + finally: |
| 118 | + left.close() |
| 119 | + right.close() |
| 120 | + |
| 121 | + |
| 122 | +rsloop.run(main()) |
| 123 | +``` |
| 124 | + |
| 125 | +Use this style when you need lower-level control than streams provide. |
| 126 | + |
| 127 | +## Start a TCP server |
| 128 | + |
| 129 | +`rsloop` supports the normal protocol and transport style: |
| 130 | + |
| 131 | +```python |
| 132 | +import asyncio |
| 133 | +import rsloop |
| 134 | + |
| 135 | + |
| 136 | +class EchoProtocol(asyncio.Protocol): |
| 137 | + def connection_made(self, transport: asyncio.BaseTransport) -> None: |
| 138 | + self.transport = transport |
| 139 | + |
| 140 | + def data_received(self, data: bytes) -> None: |
| 141 | + self.transport.write(data.upper()) |
| 142 | + |
| 143 | + |
| 144 | +async def main() -> None: |
| 145 | + loop = asyncio.get_running_loop() |
| 146 | + server = await loop.create_server(EchoProtocol, "127.0.0.1", 9000) |
| 147 | + try: |
| 148 | + print("serving on 127.0.0.1:9000") |
| 149 | + await asyncio.sleep(60) |
| 150 | + finally: |
| 151 | + server.close() |
| 152 | + await server.wait_closed() |
| 153 | + |
| 154 | + |
| 155 | +rsloop.run(main()) |
| 156 | +``` |
| 157 | + |
| 158 | +This is a good fit when your project already uses `asyncio.Protocol`. |
| 159 | + |
| 160 | +## Use streams |
| 161 | + |
| 162 | +Because `rsloop` can patch stream helpers, high-level stream code can stay familiar: |
| 163 | + |
| 164 | +```python |
| 165 | +import asyncio |
| 166 | +import rsloop |
| 167 | + |
| 168 | + |
| 169 | +async def handle_client(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None: |
| 170 | + data = await reader.read(100) |
| 171 | + writer.write(data.upper()) |
| 172 | + await writer.drain() |
| 173 | + writer.close() |
| 174 | + await writer.wait_closed() |
| 175 | + |
| 176 | + |
| 177 | +async def main() -> None: |
| 178 | + server = await asyncio.start_server(handle_client, "127.0.0.1", 9001) |
| 179 | + try: |
| 180 | + reader, writer = await asyncio.open_connection("127.0.0.1", 9001) |
| 181 | + writer.write(b"hello") |
| 182 | + await writer.drain() |
| 183 | + print((await reader.read(100)).decode()) |
| 184 | + writer.close() |
| 185 | + await writer.wait_closed() |
| 186 | + finally: |
| 187 | + server.close() |
| 188 | + await server.wait_closed() |
| 189 | + |
| 190 | + |
| 191 | +rsloop.run(main()) |
| 192 | +``` |
| 193 | + |
| 194 | +This is often the easiest path for developers who already use high-level `asyncio` streams. |
| 195 | + |
| 196 | +## Run blocking work in an executor |
| 197 | + |
| 198 | +Not everything has to be async: |
| 199 | + |
| 200 | +```python |
| 201 | +import asyncio |
| 202 | +import rsloop |
| 203 | + |
| 204 | + |
| 205 | +def blocking_sum() -> int: |
| 206 | + return sum(range(100000)) |
| 207 | + |
| 208 | + |
| 209 | +async def main() -> None: |
| 210 | + loop = asyncio.get_running_loop() |
| 211 | + result = await loop.run_in_executor(None, blocking_sum) |
| 212 | + print(result) |
| 213 | + |
| 214 | + |
| 215 | +rsloop.run(main()) |
| 216 | +``` |
| 217 | + |
| 218 | +Use this when you must call blocking Python code without freezing the event loop. |
| 219 | + |
| 220 | +## Run a subprocess |
| 221 | + |
| 222 | +`rsloop` also supports subprocess workflows: |
| 223 | + |
| 224 | +```python |
| 225 | +import asyncio |
| 226 | +import rsloop |
| 227 | +import sys |
| 228 | + |
| 229 | + |
| 230 | +async def main() -> None: |
| 231 | + proc = await asyncio.create_subprocess_exec( |
| 232 | + sys.executable, |
| 233 | + "-c", |
| 234 | + "print('hello from child')", |
| 235 | + stdout=asyncio.subprocess.PIPE, |
| 236 | + ) |
| 237 | + stdout, _ = await proc.communicate() |
| 238 | + print(stdout.decode().strip()) |
| 239 | + |
| 240 | + |
| 241 | +rsloop.run(main()) |
| 242 | +``` |
| 243 | + |
| 244 | +This is useful for scripts, tooling, and service code that needs to call external programs. |
| 245 | + |
| 246 | +## Where to find bigger examples |
| 247 | + |
| 248 | +For fuller examples, read the repository files in `examples/`: |
| 249 | + |
| 250 | +- `01_basics.py` |
| 251 | +- `02_fd_and_sockets.py` |
| 252 | +- `03_streams.py` |
| 253 | +- `04_unix_and_accepted_socket.py` |
| 254 | +- `05_pipes_signals_subprocesses.py` |
0 commit comments