Skip to content

Commit 580e891

Browse files
committed
Docs: add an example using the multiprocessing module
1 parent 497ec2d commit 580e891

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

CHANGELOG

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ History:
55
5.0.0 2019/xx/xx
66
- removed support for Python 2.7
77
- MSS: improve type annotations and add CI check
8+
- doc: add an example using the multiprocessing module (closes #82)
89

910
4.0.2 2019/02/23
1011
- new contributor: foone
11-
- Windows: ignore missing SetProcessDPIAware() on Window XP
12+
- Windows: ignore missing SetProcessDPIAware() on Window XP (fixes #109)
1213

1314
4.0.1 2019/01/26
1415
- Linux: fix several XLib functions signature (fixes #92)

docs/source/examples.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,21 @@ Benchmark
129129
Simple naive benchmark to compare with `Reading game frames in Python with OpenCV - Python Plays GTA V <https://pythonprogramming.net/game-frames-open-cv-python-plays-gta-v/>`_:
130130

131131
.. literalinclude:: examples/fps.py
132-
:lines: 12-
132+
:lines: 9-
133133

134134
.. versionadded:: 3.0.0
135135

136+
Multiprocessing
137+
---------------
138+
139+
Performances can be improved by delegating the PNG file creation to a specific worker.
140+
This is a simple example using the :py:mod:`multiprocessing` inspired by the `TensorFlow Object Detection Introduction <https://github.com/pythonlessons/TensorFlow-object-detection-tutorial>`_ project:
141+
142+
.. literalinclude:: examples/fps_multiprocessing.py
143+
:lines: 9-
144+
145+
.. versionadded:: 5.0.0
146+
136147

137148
BGRA to RGB
138149
===========
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
This is part of the MSS Python's module.
3+
Source: https://github.com/BoboTiG/python-mss
4+
5+
Example using the multiprocessing module to speed-up screen capture.
6+
https://github.com/pythonlessons/TensorFlow-object-detection-tutorial
7+
"""
8+
9+
from multiprocessing import Process, Queue
10+
11+
import mss
12+
import mss.tools
13+
14+
15+
def grab(queue):
16+
# type: (Queue) -> None
17+
18+
rect = {"top": 0, "left": 0, "width": 600, "height": 800}
19+
20+
with mss.mss() as sct:
21+
for _ in range(1_000):
22+
queue.put(sct.grab(rect))
23+
24+
# Tell the other worker to stop
25+
queue.put(None)
26+
27+
28+
def save(queue):
29+
# type: (Queue) -> None
30+
31+
number = 0
32+
output = "screenshots/file_{}.png"
33+
to_png = mss.tools.to_png
34+
35+
while "there are screenshots":
36+
img = queue.get()
37+
if img is None:
38+
break
39+
40+
to_png(img.rgb, img.size, output=output.format(number))
41+
number += 1
42+
43+
44+
if __name__ == "__main__":
45+
# The screenshots queue
46+
queue = Queue() # type: Queue
47+
48+
# 2 processes: one for grabing and one for saving PNG files
49+
Process(target=grab, args=(queue,)).start()
50+
Process(target=save, args=(queue,)).start()

0 commit comments

Comments
 (0)