Wolfy
Wolfy

Reputation: 1465

Shared lock for Python objects

I'm developing a tiny web application with Flask/Gunicorn on Heroku. Since I'm just prototyping, I have a single web process (dyno) with a worker thread started by the same process. The web application is just returning a JSON dump of a global object, which is periodically updated by the worker thread monitoring an external web service. The global object is updated every 15 to 60 minutes. My plan was to use an exclusive lock in the worker thread when an update to the global object is needed, and a shared lock in the web threads so multiple requests can be satisfied concurrently. Unfortunately it looks like that Python doesn't have shared locks, only exclusive locks. How can I ensure consistency in the web threads, i.e., how to be sure that the update to the global object is atomic while allowing multiple read-only access to the object?

Upvotes: 0

Views: 1443

Answers (1)

Nathan Binkert
Nathan Binkert

Reputation: 9124

Since the updates are so infrequent, you're better off just making a copy of the object, updating copy, and then updating the global variable to point to the new object. Simple assignments in python are atomic so you don't need any locks at all.

Upvotes: 1

Related Questions