非デーモンのpythonプールを作成することは可能でしょうか?私は、プール内に別のプールを持つ関数を呼び出せるようにしたい。ありがとう。Python Process Pool非デーモンですか?
答えて
multiprocessing.pool.Pool
クラスは鬼神のそれらを作り、それらを開始し、その__init__
方法でワーカープロセスを作成し、それらが開始されている(そしてその後それはもう許されていない)の前に、False
に自分daemon
属性を再設定することはできません。しかし、独自のサブクラスmultiprocesing.pool.Pool
(multiprocessing.Pool
は単なるラッパー関数です)を作成して、独自のmultiprocessing.Process
サブクラス(これは常にデーモンではない)をワーカープロセスに使用することができます。
ここでは、これを行う方法の完全な例を示します。重要な部分は、NoDaemonProcess
とMyPool
の2つのクラスで、最後にMyPool
インスタンスのpool.close()
とpool.join()
を呼び出します。
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import multiprocessing
# We must import this explicitly, it is not imported by the top-level
# multiprocessing module.
import multiprocessing.pool
import time
from random import randint
class NoDaemonProcess(multiprocessing.Process):
# make 'daemon' attribute always return False
def _get_daemon(self):
return False
def _set_daemon(self, value):
pass
daemon = property(_get_daemon, _set_daemon)
# We sub-class multiprocessing.pool.Pool instead of multiprocessing.Pool
# because the latter is only a wrapper function, not a proper class.
class MyPool(multiprocessing.pool.Pool):
Process = NoDaemonProcess
def sleepawhile(t):
print("Sleeping %i seconds..." % t)
time.sleep(t)
return t
def work(num_procs):
print("Creating %i (daemon) workers and jobs in child." % num_procs)
pool = multiprocessing.Pool(num_procs)
result = pool.map(sleepawhile,
[randint(1, 5) for x in range(num_procs)])
# The following is not really needed, since the (daemon) workers of the
# child's pool are killed when the child is terminated, but it's good
# practice to cleanup after ourselves anyway.
pool.close()
pool.join()
return result
def test():
print("Creating 5 (non-daemon) workers and jobs in main process.")
pool = MyPool(5)
result = pool.map(work, [randint(1, 5) for x in range(5)])
pool.close()
pool.join()
print(result)
if __name__ == '__main__':
test()
上記のコードは私のためにぶら下がっているようです。具体的には、work()内のpool.close()でハングしているようです。私は行方不明のものはありますか? –
LinuxとPython 2.6/2.7/3.2 OS XでPython 2.7/3.2(print行を修正した後)で自分のコードをもう一度テストしました。LinuxとPython 2.7/3.2はOS X上でうまく動作しますが、 OS X(Lion)上のPython 2.6で動作します。これは修正されたマルチプロセッシングモジュールのバグだと思われますが、実際にはバグトラッカーをチェックしていません。 –
これは実際にはマルチプロセッシングモジュールで修正する必要があります(非デーモンワーカーのオプションが利用可能である必要があります)。誰がそれを維持しているか知っていますか? –
multiprocessingモジュールは、プロセスまたはスレッドとプールを使用するにはすてきなインターフェイスを持っています。あなたの現在のユースケースによっては、外側のプールにmultiprocessing.pool.ThreadPool
を使用することを検討すると、プロセスではなくのスレッドを(内部からプロセスを起動できるスレッド)にします。作成したとしてそれはGILによって、私の特定のケース(私は両方をテストした)、プロセスの起動時間に外Pool
から制限される可能性があります
hereははるかThreadPool
でソリューションを上回ります。
それはThreads
ためProcesses
を交換するのはとても簡単です。 ThreadPool
ソリューションの使用方法の詳細については、hereまたはhereをご覧ください。
問題はモジュール間でグローバルをインポートしようとしたときに発生し、ProcessPool()行が複数回評価されていました。
はglobals.py
from processing import Manager, Lock
from pathos.multiprocessing import ProcessPool
from pathos.threading import ThreadPool
class SingletonMeta(type):
def __new__(cls, name, bases, dict):
dict['__deepcopy__'] = dict['__copy__'] = lambda self, *args: self
return super(SingletonMeta, cls).__new__(cls, name, bases, dict)
def __init__(cls, name, bases, dict):
super(SingletonMeta, cls).__init__(name, bases, dict)
cls.instance = None
def __call__(cls,*args,**kw):
if cls.instance is None:
cls.instance = super(SingletonMeta, cls).__call__(*args, **kw)
return cls.instance
def __deepcopy__(self, item):
return item.__class__.instance
class Globals(object):
__metaclass__ = SingletonMeta
"""
This class is a workaround to the bug: AssertionError: daemonic processes are not allowed to have children
The root cause is that importing this file from different modules causes this file to be reevalutated each time,
thus ProcessPool() gets reexecuted inside that child thread, thus causing the daemonic processes bug
"""
def __init__(self):
print "%s::__init__()" % (self.__class__.__name__)
self.shared_manager = Manager()
self.shared_process_pool = ProcessPool()
self.shared_thread_pool = ThreadPool()
self.shared_lock = Lock() # BUG: Windows: global name 'lock' is not defined | doesn't affect cygwin
は、その後ノーそれは、プール内のすべての労働者はデーモン化していることはできません、あなたのコード内の別の場所から安全に
from globals import Globals
Globals().shared_manager
Globals().shared_process_pool
Globals().shared_thread_pool
Globals().shared_lock
- 1. デーモンpythonラッパー "サブプロセスI/Oタイムアウト"
- 2. python c extension/openclデーモン
- 3. デーモンとPythonスクリプトのアップスタート
- 4. 効率的なPythonデーモン
- 5. 非特権ユーザーとしてpythonデーモンを実行し、グループメンバシップを維持する
- 6. boost :: details :: pool :: pthread_mutexとboost :: details :: pool :: null_mutex
- 7. Pythonデーモンを作成するデファクトライブラリとは何ですか
- 8. autorelease pool
- 9. boost :: pool <> :: mallocとboost :: pool <> :: ordered_mallocの違いは何ですか?boost :: pool <> :: ordered_mallocはいつ使うべきですか?
- 10. Linuxサービス/デーモンとしてのPythonスクリプト
- 11. ubuntuのデーモンとしてのPythonスクリプト
- 12. Visio Process Simulatorとは何ですか?
- 13. PHPスクリプトからPythonデーモンと通信する方法
- 14. Win32 :: Process :: Memoryと同等のPythonでのプロセスメモリ検索
- 15. デーモンのないWebSocketサーバーですか?
- 16. Pythonデーモンを使ってOS X 10.7でアクティブなアプリケーションを取得する
- 17. pep-3143でpython 2.7デーモンを作成する
- 18. ubuntuでpython apschedulerをデーモンとしてインストールするには?
- 19. Pythonで2つのデーモンを接続する
- 20. WebSphere JDBC Connection Poolアドバイス
- 21. Mapped Buffer Pool/Direct Buffer Poolとはどのようにしてサイズを増やすのですか?
- 22. SinatraでRack :: Session :: Poolを使用する
- 23. worker vs process vs clientとjobとCelery Pythonモジュールのタスクexplaination
- 24. WebSphere Process Serverからのログ
- 25. Python - Mac OS X上でGUIアプリケーションからデーモンを作成するには?
- 26. apschdulerをPythonスクリプトでデーモンとして実行していますか?
- 27. windows process memory layout
- 28. Java Killing Popup Process
- 29. Android Process Viewer
- 30. Jmeter OS Process Sampler
私の知る限りをインポートし、それが__injectすることはできませんdependency__、BTW私はあなたの質問の2番目の部分を理解していません。私は、プール内に別のプールがある関数を呼び出すことができるようにして、労働者がデーモン化されているという事実を妨げる方法を理解しています。 – mouad
関数aに関数bを実行するプールがあり、関数cを実行するプールがある場合、bにはデーモンプロセスで実行されており、デーモンプロセスはプロセスを作成できないという問題があります。 'AssertionError:デーモンプロセスは子供を持つことができません ' – Max