Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add asyncio support to publish function #336

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions fedora_messaging/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import inspect
import logging
import asyncio

import crochet
from twisted.internet import defer, reactor
Expand Down Expand Up @@ -310,17 +311,20 @@ def publish(message, exchange=None, timeout=30):
exchange = config.conf["publish_exchange"]

eventual_result = _twisted_publish(message, exchange)
try:
eventual_result.wait(timeout=timeout)
publish_signal.send(publish, message=message)
except crochet.TimeoutError:
eventual_result.cancel()
wrapper = exceptions.PublishTimeout(
f"Publishing timed out after waiting {timeout} seconds."
)
publish_failed_signal.send(publish, message=message, reason=wrapper)
raise wrapper
except Exception as e:
_log.error(eventual_result.original_failure().getTraceback())
publish_failed_signal.send(publish, message=message, reason=e)
raise
async def wait_for_result():
try:
await asyncio.wait_for_result(eventual_result, timeout=timeout)
publish_signal.send(publish, message=message)
except asyncio.TimeoutError:
eventual_result.cancel()
wrapper = exceptions.PublishTimeout(
f"Publishing timed out after waiting {timeout} seconds."
)
publish_failed_signal.send(publish, message=message, reason=wrapper)
raise wrapper
except Exception as e:
_log.error(eventual_result.original_failure().getTraceback())
publish_failed_signal.send(publish, message=message, reason=e)
raise

asyncio.run(wait_for_result())
Loading