Skip to content

Instantly share code, notes, and snippets.

@sunfkny
Created March 26, 2024 02:24
Show Gist options
  • Save sunfkny/2e61625794ddbcf260c38fd54193a000 to your computer and use it in GitHub Desktop.
Save sunfkny/2e61625794ddbcf260c38fd54193a000 to your computer and use it in GitHub Desktop.
# https://github.com/Bogdanp/dramatiq
# start worker: dramatiq <module.name>
import sys
import dramatiq
import requests
from dramatiq.brokers.redis import RedisBroker
from dramatiq.middleware import CurrentMessage
from dramatiq.results import Results
from dramatiq.results.backends.redis import RedisBackend
redis_broker = RedisBroker()
redis_broker.add_middleware(CurrentMessage())
redis_broker.add_middleware(Results(backend=RedisBackend()))
dramatiq.set_broker(redis_broker)
@dramatiq.actor(store_results=True)
def count_words(url: str):
response = requests.get(url)
count = len(response.text.split(" "))
message_id = getattr(CurrentMessage.get_current_message(), "message_id")
print(f"[{message_id}] There are {count} words at {url!r}.")
return count
if __name__ == "__main__":
result = count_words.send(sys.argv[1])
print(result.message_id)
print(result.get_result(block=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment