yohhoyの日記

技術的メモをしていきたい日記

mutexモジュールはマルチスレッド処理には使えない

2020-01-01をもって Python2はEOL を迎えている。

Python 2.x系にある mutexモジュール は、(その名前に反して)マルチスレッド処理には使えない。同モジュールはPython 2.6以降で非推奨、Python 3系では削除された。代わりに threading.Lockオブジェクト を利用すること。

公式ドキュメント mutex - 排他制御 より一部引用。

バージョン 2.6 で撤廃: mutex モジュールは Python 3.0 で削除されました。

当然のことながらマルチスレッドの制御には利用できません – というのも、lock()が、ロックを獲得したら関数を呼び出すという変なインタフェースだからです。

Python 2→3で標準ライブラリを整理を行った、PEP 3108 - Standard Library Reorganizationより一部引用。

Hardly used [done]
Some platform-independent modules are rarely used. There are a number of possible explanations for this, including, ease of reimplementation, very small audience or lack of adherence to more modern standards.

mutex

  • Easy to implement using a semaphore and a queue.
  • Cannot block on a lock attempt.
  • Not uniquely edited since its addition 15 years ago.
  • Only useful with the 'sched' module.
  • Not thread-safe.

関連URL