How can I run myscript.py after the transaction manager has returned. Additionally, I would prefer if the script was not blocking.
In my view, I am receiving a file from a POST. Since I'm creating the file with repoze.filesafe
's create_file()
, it keeps the file in a temporary location until the transaction manager returns. The file only exists on the harddisk in its correct path after transaction manager has returned without an error.
Therefore, I need to run my script after the transaction manager has returned.
You can register a hook to be run after commit via the transaction
package. Register one in your view:
import transaction
def your_after_commit(success, arg1, arg2, kwarg1=None, kwarg2=None):
if success:
print "Transaction commit succeeded"
else:
print "Transaction commit failed"
def someview(request):
current_transaction = transaction.get()
current_transaction.addAfterCommitHook(your_after_commit, args=(1, 2), kws={kwarg1='foo', kwargs2='bar'})
This still runs your script in the context of the current request (e.g. the request does not complete until your script returns). If you need a full asynchronous setup, you'll need to move to a proper asynchronous solution such as Celery. You would not use that with a transaction hook; just register a task to be run with Celery instead.