Skip to content

Commit

Permalink
docs: update example in introduction [python] (#2677)
Browse files Browse the repository at this point in the history
  • Loading branch information
cagejsn authored Aug 8, 2024
1 parent 0faec37 commit c67ce33
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions docs/gitbook/python/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,36 @@ In order to consume the jobs from the queue you need to use the `Worker` class,

```python
from bullmq import Worker
import asyncio
import signal

async def process(job, job_token):
# job.data will include the data added to the queue
return doSomethingAsync(job)

async def main():

# Create an event that will be triggered for shutdown
shutdown_event = asyncio.Event()

def signal_handler(signal, frame):
print("Signal received, shutting down.")
shutdown_event.set()

# Assign signal handlers to SIGTERM and SIGINT
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)

# Feel free to remove the connection parameter, if your redis runs on localhost
worker = Worker("myQueue", process, {"connection": "rediss://<user>:<password>@<host>:<port>"})

# This while loop is just for the sake of this example
# you won't need it in practice.
while True: # Add some breaking conditions here
await asyncio.sleep(1)
# Wait until the shutdown event is set
await shutdown_event.wait()

# When no need to process more jobs we should close the worker
# close the worker
print("Cleaning up worker...")
await worker.close()
print("Worker shut down successfully.")

if __name__ == "__main__":
asyncio.run(main())
Expand Down

0 comments on commit c67ce33

Please sign in to comment.