Skip to content
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

Threadsafety improvements using thread local storage #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dhionel
Copy link

@dhionel dhionel commented May 15, 2012

When mechanize is used in a heavily threaded context, serious contention and threadsafety concerns arise, related to the use of a global _opener object.

The proposed changes place _opener in thread.local storage, eliminating the contention. They has been tested in a scenario where each thread only uses its own mechanize objects, that's enough to support a stress test framework like multi-mechanize (http://testutils.org/multi-mechanize/) in a heavily threaded context. With those changes in the general case, if a thread uses mechanize objects created by another one it would use its local _opener object. If that is ok, there should be no issues.

Complete threadsafety is required for the most general case, and more testing. In the restricted scenario the proposed changes seem to be enough for a stable behaviour.

The _opener global object defined in the _opener module was wrapped in a
subclass of threading.local to improve mechanize threadsafety. With this
change that object is no longer shared between threads and is not required
to synchronize the execution of urlopen and urlretrieve.

This modified version of mechanize was used successfully as backend for a stress
test based on the multi-mechanize framework (*) in which up to 512 agents per
user group were active.

(*) http://code.google.com/p/multi-mechanize/

Signed-off-by: Dhionel Díaz <ddiaz@cenditel.gob.ve>
@jamesbroadhead
Copy link

jamesbroadhead commented Mar 12, 2017

Thank you for your contribution to mechanize!

Following the process in #117, future work on mechanize will be occurring here:
https://github.com/python-mechanize/mechanize.

Please re-file your PR there (where it will get attention, and hopefully merged)

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