2

I am trying to use threads in Odoo 14 for the first time and I would like to ask for some basics and common pitfalls.

I found surprisingly little on this topic online. Even official docs basically say: don't do this unless you are 1000% sure you know what you are doing, but they don't provide any resources on how to learn it.

Key points I would like to learn:

  • How to read, write, create and unlink records in a new thread?
  • When to commit and when to rollback?
  • What is cr.savepoint()?

My usecase: I have 2000 products for which I need to get the current price every day from 5 different vendor e-shops with HTTP requests. This process will be run from CRON and cannot be blocking, because it might take a long time to complete.

Any help would be appreciated. Even if you answer only part of my questions I would be glad.

xixo222
  • 157
  • 2
  • 9
  • Have you thought about using an asychnronous approach for that? For example by using [the OCA queue framework](https://github.com/OCA/queue)? You can easily create simple jobs, e.g. a job for getting a price for one product, and this framework will already use Odoo workers and try to distribute the load (lot of jobs when they are simple) to them, if there are enough. So nothing you have to do about threading and everything additionally to that. – CZoellner Feb 09 '23 at 12:28
  • Some basic documentation can be found in the [queue_job module](https://github.com/OCA/queue/tree/14.0/queue_job). – CZoellner Feb 09 '23 at 12:30
  • 1
    @CZoellner you are right, OCA queue framework is quite an elegant solution for my use case. I added my use case just for context for possible answers, but thank you so much for your solution nevertheless. – xixo222 Feb 09 '23 at 14:36
  • One more nice advantage of using async jobs is to shorten the lifespan of cron workers in Odoo. In my experience long living cron workers tend to make a lot of problems and they have a poor design when it comes to exception handling (rollback and kill...) or trying to build something like a chain between crons. – CZoellner Feb 09 '23 at 16:16

0 Answers0