j***@gmail.com
2018-06-01 03:34:42 UTC
Hi,
Within a view function it becomes necessary to run an async task. Celery
<http://www.celeryproject.org/>, Dramatiq <https://dramatiq.io/>, etc are
frameworks to handle that. However, my async task needs to read db objects
which have been createdâand must be committed and persistedâby the view
function that spawns the task. Thus, I need to delay spawning the task
(i.e. sending the taskâs message to the workers) until after the requestâs
db transaction has committed.
The db transaction is a standard SQLAlchemy transaction. Using the commit
veto hook
<https://docs.pylonsproject.org/projects/pyramid_tm/en/latest/#adding-a-commit-veto-hook>
is too early. This SO answer
<https://stackoverflow.com/questions/17499171/pyramid-zopetransactionextension-with-celery-how-can-i-commit-transaction-immed#17507093>
suggests using the repoze.tm.after_end()
<http://repozetm2.readthedocs.io/en/latest/#cleanup> callback, but the
answer is about five years old now.
How do I handle this scenario correctly, i.e. how do I schedule an async
job after a request was successful? I guess I can pack up the task object,
or a signature, and its parameters and attach them to the request from
within the view function. But where is the right place to actually send the
task message?
Thanks!
Jens
Within a view function it becomes necessary to run an async task. Celery
<http://www.celeryproject.org/>, Dramatiq <https://dramatiq.io/>, etc are
frameworks to handle that. However, my async task needs to read db objects
which have been createdâand must be committed and persistedâby the view
function that spawns the task. Thus, I need to delay spawning the task
(i.e. sending the taskâs message to the workers) until after the requestâs
db transaction has committed.
The db transaction is a standard SQLAlchemy transaction. Using the commit
veto hook
<https://docs.pylonsproject.org/projects/pyramid_tm/en/latest/#adding-a-commit-veto-hook>
is too early. This SO answer
<https://stackoverflow.com/questions/17499171/pyramid-zopetransactionextension-with-celery-how-can-i-commit-transaction-immed#17507093>
suggests using the repoze.tm.after_end()
<http://repozetm2.readthedocs.io/en/latest/#cleanup> callback, but the
answer is about five years old now.
How do I handle this scenario correctly, i.e. how do I schedule an async
job after a request was successful? I guess I can pack up the task object,
or a signature, and its parameters and attach them to the request from
within the view function. But where is the right place to actually send the
task message?
Thanks!
Jens
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+***@googlegroups.com.
To post to this group, send email to pylons-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/ef6bf67d-acec-4dae-b318-7749743add31%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discuss+***@googlegroups.com.
To post to this group, send email to pylons-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/ef6bf67d-acec-4dae-b318-7749743add31%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.