Thread-safety in Async Worker #1122
-
Hello, @miguelgrinberg. I have seen in the documentation that I have read several issues about it:
But I am still not sure, whether I should use asynchronous Lock and where? So in short:
The question is:
Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
In general I'd say the answer is yes for 1 and no for 2. But really you need to look at this in detail to determine exactly when you need to lock. Given that you are using a message queue, the first consideration is wether the writes to the message queue are safe to issue from multiple tasks. If they are, then the emits from your background jobs should be safe as well. If your server will never have two background jobs emitting at the same time, then this is also a good indicator that you can get away without using locks. The external workers should be fine as long as they don't emit from multiple tasks. |
Beta Was this translation helpful? Give feedback.
In general I'd say the answer is yes for 1 and no for 2.
But really you need to look at this in detail to determine exactly when you need to lock. Given that you are using a message queue, the first consideration is wether the writes to the message queue are safe to issue from multiple tasks. If they are, then the emits from your background jobs should be safe as well.
If your server will never have two background jobs emitting at the same time, then this is also a good indicator that you can get away without using locks.
The external workers should be fine as long as they don't emit from multiple tasks.