Created
March 26, 2024 02:24
-
-
Save sunfkny/2e61625794ddbcf260c38fd54193a000 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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