Django views thread-safety?
I have several view functions that process data from remote sources. In many cases this processing can take over a second to complete. Will simultaneous access to these view functions potentially scramble my data?
In addition, I will have a continually-running background thread that will be populating my database. This background thread will be calling some of the same library functions that my view functions will be calling. Is this a poten开发者_开发百科tial thread-safety issue?
If yes, what are the best practices? I'm assuming just using python's locking mechanism will work, but are there better approaches?
Thanks!
-Travis
For long running processes, use Celery.
For a long view, you can create a model instance, and start a celery task which populates it. A view can show the status of this instance, with self refreshing html until the results are there.
I don't see you doing anything there that would obviously cause any threading issues. Thread safety is more like if you wanted to parallelize the processing of a single request, then you'd have to make sure the mutiple threads for one request where thread safe.
In your case each request is accessing data and will get it's own copy.
You're reading/writing data from remote sources. I would try to synchronize access or use transactions at these remote sources, not at the client!
精彩评论