I am trying to call a function after a response has been returned from a http request:
class SomeAPIEndpoint(http.Controller):
@http.route('/api', type='json')
def controller(self, **kwargs):
data = self.function1(request)
with api.Environment.manage():
env = api.Environment(request.env.cr, request.env.user.id, request.env.context)
t = threading.Thread(target=self.function2, args=(env,))
t.start()
return data
def function1(self, request):
# should return the response
data = request.env['some.model'].search([])
# work with the data here ...
return data
def function2(self, env):
# should not stop the response
# triggers a cron job that gets new data from a service
# and stores the data in some.model
cron = env['ir.cron'].search([('id', '=', 1)])
cron.with_user(cron.user_id).with_context(lastcall=cron.lastcall).ir_actions_server_id.run()
I want function1
to be executed and return the response and function2
to be executed after the response.
But I get Unable to use a closed cursor.
How can I solve this?
Thank you