The Wayback Machine - https://web.archive.org/web/20201212151032/https://github.com/BoboTiG/python-mss/pull/159
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

Windows: fix multi-thread unsafe and update test #159

Merged
merged 8 commits into from Apr 28, 2020

Conversation

@narumishi
Copy link
Contributor

@narumishi narumishi commented Apr 27, 2020

Changes proposed in this PR

  • Fixes #150
  • replace srcdc with srcdc_dict to maintain srcdc values created by multiple threads
  • add a threading lock MSS._lock to ensure multi-thread safe

It is very important to keep up to date tests and documentation.

  • Tests added/updated
  • Documentation updated

Is your code right?

  • PEP8 compliant
  • flake8 passed
On Windows, the handle of entire window device context is saved to
`srcdc`. But the device context will be released once the thread who
creates it has died, so that `srcdc` is no loner valid. Replace `srcdc`
with `srcdc_dict` to maintain srcdc values created by multiple threads
which ensure the validity of srcdc when it's used.

A threading lock is add to prevent multiple threads from grabbing and
modifying shared class attributes `bmp`/`srcdc`/`memdc` (their windows
object in fact) at same time. Otherwise, unexpected screenshot or
unpredictable error will occur.

Add test_thread_safety in test_windows.py

Closes #150
Copy link
Owner

@BoboTiG BoboTiG left a comment

Thanks a lot @narumishi :)

CHANGELOG Outdated Show resolved Hide resolved
CHANGES.rst Outdated Show resolved Hide resolved
CHANGES.rst Outdated Show resolved Hide resolved
mss/tests/test_windows.py Outdated Show resolved Hide resolved
mss/tests/test_windows.py Outdated Show resolved Hide resolved
mss/windows.py Outdated Show resolved Hide resolved
mss/windows.py Outdated Show resolved Hide resolved
BoboTiG added 3 commits Apr 28, 2020
BoboTiG added 4 commits Apr 28, 2020
@BoboTiG BoboTiG merged commit 80aa06e into BoboTiG:master Apr 28, 2020
1 check failed
1 check failed
Sourcery Refactorings found
Details
@narumishi
Copy link
Contributor Author

@narumishi narumishi commented Apr 28, 2020

Sorry @BoboTiG , I have forgotten to remove the wrong comment Every loop will take about 1 second. in test_windows.py. Please update it later.

If one second per loop, the exception caused by resouces written by multiple threads at same time will occur at a very low possibility. So I removed time.sleep() in the test but forgot to update comment.

@BoboTiG
Copy link
Owner

@BoboTiG BoboTiG commented Apr 28, 2020

OK thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.