Skip to content

Commit 33c9792

Browse files
committed
Merge #223: Add a 'getting started' section to the README
Approved-by: Qqwy Priority: Normal Auto-deploy: false
2 parents f7e55e8 + e19f944 commit 33c9792

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,93 @@ The specific advantages for opsqueue are:
1818
* One standardized queuing system that can be reused again and again
1919
* A single way to implement monitoring, alerting, and debugging workflows
2020

21+
## Getting Started:
22+
23+
### 1. Grab the `opsqueue` binary and the Python client library
24+
25+
The binary can be installed from this repo, or build it from source by cloning the repo using `just build` or `cargo build`.
26+
This is a self-contained program, you can run it on a server on its own, include it in a tiny container, etc.
27+
28+
The Python library used by the `Consumer` and `Producer` can be built from source, or (soon) simply be installed from pypi. (`pip install opsqueue`,`uv install opsqueue` etc.)
29+
30+
### 2. Create a `Producer`
31+
32+
```python
33+
import logging
34+
from opsqueue.producer import ProducerClient
35+
from collections.abc import Iterable
36+
37+
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.DEBUG)
38+
39+
def file_to_words(filename: str) -> Iterable[str]:
40+
"""
41+
Iterates over each word and inter-word whitespace strings in a file
42+
while keeping at most one line in memory at a time.
43+
"""
44+
with open(filename) as input_file:
45+
for line in input_file:
46+
for word in line.split():
47+
yield word
48+
49+
def print_words(words: Iterable[str]) -> None:
50+
"""
51+
Prints all words and inter-word whitespace tokens
52+
without first loading the full string into memory
53+
"""
54+
for word in words:
55+
print(word, end="")
56+
57+
def main() -> None:
58+
client = ProducerClient("localhost:3999", "file:///tmp/opsqueue/capitalize_text/")
59+
stream_of_words = file_to_words("lipsum.txt")
60+
stream_of_capitalized_words = client.run_submission(stream_of_words, chunk_size=4000)
61+
print_words(stream_of_capitalized_words)
62+
63+
if __name__ == "__main__":
64+
main()
65+
```
66+
67+
### 3. Create a `Consumer`
68+
69+
```python
70+
import logging
71+
from opsqueue.consumer import ConsumerClient, Strategy
72+
73+
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
74+
75+
def capitalize_word(word: str) -> str:
76+
output = word.capitalize()
77+
# print(f"Capitalized word: {word} -> {output}")
78+
return output
79+
80+
def main() -> None:
81+
client = ConsumerClient("localhost:3999", "file:///tmp/opsqueue/capitalize_text/")
82+
client.run_each_op(capitalize_word, strategy=Strategy.Random())
83+
84+
if __name__ == "__main__":
85+
main()
86+
```
87+
88+
89+
4. Run the Producer, queue and Consumer
90+
91+
- Run `opsqueue`.
92+
- Run `python3 capitalize_text_consumer.py` to run a consumer. Feel free to start multiple instances of this program to try out consumer concurrency.
93+
- Run `python3 capitalize_text_producer.py` to run a producer.
94+
95+
The order you start these in does not matter; systems will reconnect and continue after any kind of failure or disconnect.
96+
97+
By default the queue will listen on `http://localhost:3999`. The exact port can of course be changed.
98+
Producer and Consumer need to share the same object store location to store the content of their submission chunks.
99+
In development, this can be a local folder as shown in the code above.
100+
In production, you probably want to use Google's GCS, Amazon's S3 or Microsoft's Azure buckets.
101+
102+
Please tinker with above code!
103+
If you want more logging to look under the hood, run `RUST_LOG=debug opsqueue` to enable extra logging for the queue.
104+
The Producer/Consumer will use whatever log level is configured in Python.
105+
106+
More examples can be found in `./libs/opsqueue_python/examples/`
107+
21108
## Project structure
22109

23110
The majority of Opsqueue's code is written in Rust.

0 commit comments

Comments
 (0)