How to execute python script in background from odoo

Viewed 27

In my odoo app I want to execute a python script on a button click and I want it to run in background.I tried using subprocess library but it only runs in the foreground.

 result = subprocess.run(["python3", "/path-to-script/script.py"], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False) 

Is there any way to execute a python script from odoo in the background.

1 Answers

The best way to do that in back-office is to create a "Server Action", which then will be added to the back-office "Action"-menu (in all views related to the chosen model):

Go to Configuration > Turn url into debug modus by adding ?debug=1 to your url: https://your-domain.dev.odoo.com/web?debug=1#action=8&model=ir.actions.server&view_type=list&cids=1&menu_id=4

In the last item of the Top menu, Click on Server-Action.

Click on the CREATE button:

  • Choose the model, you want to add your Action-link to execute your script : Payment - in this example.

  • As executed action, Choose : Python. Then, in the bottom panel: add your python script or call your model-def:

    action = model.action_register_payment()

Example:

enter image description here

Under the top green button : CREATE / UPDATE, click on ADD AS CONTEXT ACTION, so that in will be added to the Action Menu on the corresponding model in back-office (in all related views). The Action menu will appear in those views after you have checked records in the list-view for example:

enter image description here

Related