Skip to content

Add a lock for folks using threading. Mucho helpful. #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 19, 2014

Conversation

theengineear
Copy link
Contributor

I can't see any downside to including a lock here. Basically, it
just says that two threads running in parallel can't touch the code
between the lock.acquire() <--> lock.release() statements
concurrently. We use it in low-level util functions to prevent
concurrent file reads/writes.

This was a bug that showed up while running our auto-documentation
examples.

I can't see any *downside* to including a lock here. Basically, it
just says that two threads running in parallel can't touch the code
between the lock.acquire() <--> lock.release() statements
concurrently. We use it in low-level util functions to prevent
concurrent file reads/writes.

This was a bug that showed up while running our auto-documentation
examples.
@theengineear
Copy link
Contributor Author

@etpinard , @chriddyp . This came up while running a bazillion Plotly requests concurrently for generating auto-documentation. Fix works like a charm. Also, threading is built-in so we don't need to add it as a requirement. Also, I can't see how an exception can be thrown between the acquire() and release() statements as it's written, so no one using threads will get stuck. Also, if you're just using this sequentially (like most) you won't notice a difference.

@@ -39,8 +44,10 @@ def save_json_dict(filename, json_dict):
"""Will error if filename is not appropriate, but it's checked elsewhere.
"""
if isinstance(json_dict, dict):
lock.acquire()
with open(filename, "w") as f:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops. we assume this directory is accessible here. I'm pretty sure there's a check for this higher-up, but that's not obvious and should be changed.

for fn in [CREDENTIALS_FILE, CONFIG_FILE]:
utils.ensure_file_exists(fn)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is clearer IMO since we're loading from it below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed 👍

@etpinard
Copy link
Contributor

👯

@theengineear
Copy link
Contributor Author

two dancers! my lucky day!

theengineear added a commit that referenced this pull request Sep 19, 2014
Add a lock for folks using threading. Mucho helpful.
@theengineear theengineear merged commit 1f5edc9 into master Sep 19, 2014
@theengineear theengineear deleted the andrew-lock-file-read-write branch September 19, 2014 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants