Skip to content

Commit

Permalink
Fix issues related to 'concurrent.futures' on Python 3.7 and 3.8 (close
Browse files Browse the repository at this point in the history
  • Loading branch information
flozz committed Oct 14, 2022
1 parent 69f6bfc commit b894c81
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ __env__
/images/
/misc/
__misc__
python/
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ Changelog
thumbnails from the cache instead of generating them from original images
* Update the code to not use deprecated constants on newer Pillow versions
* Various typo fixed (@kianmeng, #31)
* Fix issues related to ``concurrent.futures`` on Python 3.7 and 3.8 (#32)

* **v1.1.2:**

Expand Down
9 changes: 8 additions & 1 deletion yoga_image_optimizer/stoppable_process_pool_executor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import functools
import multiprocessing
from concurrent.futures import ProcessPoolExecutor
Expand Down Expand Up @@ -28,9 +29,15 @@ def __init__(self, *args, **kwargs):

def shutdown(self, *args, **kwargs):
processes = self._processes
ProcessPoolExecutor.shutdown(self, *args, **kwargs)

# Python < 3.9: We should wait else we got an OSError:
# https://bugs.python.org/issue36281
if sys.version_info.major >= 3 and sys.version_info.minor < 9:
kwargs["wait"] = True

for pid, process in processes.items():
process.kill()
ProcessPoolExecutor.shutdown(self, *args, **kwargs)
self._state_manager.shutdown()

shutdown.__doc__ = ProcessPoolExecutor.shutdown.__doc__
Expand Down
9 changes: 8 additions & 1 deletion yoga_image_optimizer/thumbnailer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import CancelledError

Expand Down Expand Up @@ -152,6 +153,12 @@ def cancel(self, uuid):
del self._pending[uuid]

def cancel_all(self):
# Python < 3.9: futures must be cancelled manually
if sys.version_info.major >= 3 and sys.version_info.minor < 9:
for future in [p["future"] for p in self._pending.values()]:
future.cancel()
self._executor.shutdown(wait=False)
else:
self._executor.shutdown(wait=False, cancel_futures=True)
self._pending = {}
self._executor.shutdown(wait=False, cancel_futures=True)
self._executor = ThreadPoolExecutor(max_workers=self._MAX_WORKERS)

0 comments on commit b894c81

Please sign in to comment.