Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python';什么是台球叉?_Python - Fatal编程技术网

python';什么是台球叉?

python';什么是台球叉?,python,Python,有人知道python的池在哪个阶段分叉主进程吗? 在创建池时,还是在第一次运行作业时?创建多处理.pool对象时,它会生成一些线程,但不会分叉。分叉是在调用其他Pool方法时完成的,并且仅在UNIX系统上(在Windows上没有分叉) 您可以在读取池时看到这一点 class Pool(object): ''' Class which supports an async version of applying functions to arguments. '''

有人知道python的池在哪个阶段分叉主进程吗?
在创建池时,还是在第一次运行作业时?

创建
多处理.pool
对象时,它会生成一些线程,但不会分叉。分叉是在调用其他
Pool
方法时完成的,并且仅在UNIX系统上(在Windows上没有分叉)

您可以在读取
池时看到这一点

class Pool(object):
    '''
    Class which supports an async version of applying functions to arguments.
    '''
    Process = Process

    def __init__(self, processes=None, initializer=None, initargs=(),
                 maxtasksperchild=None):
        self._setup_queues()
        self._taskqueue = queue.Queue()
        self._cache = {}
        self._state = RUN
        self._maxtasksperchild = maxtasksperchild
        self._initializer = initializer
        self._initargs = initargs

        if processes is None:
            try:
                processes = cpu_count()
            except NotImplementedError:
                processes = 1
        if processes < 1:
            raise ValueError("Number of processes must be at least 1")

        if initializer is not None and not callable(initializer):
            raise TypeError('initializer must be a callable')

        self._processes = processes
        self._pool = []
        self._repopulate_pool()

        self._worker_handler = threading.Thread(
            target=Pool._handle_workers,
            args=(self, )
            )
        self._worker_handler.daemon = True
        self._worker_handler._state = RUN
        self._worker_handler.start()


        self._task_handler = threading.Thread(
            target=Pool._handle_tasks,
            args=(self._taskqueue, self._quick_put, self._outqueue, self._pool)
            )
        self._task_handler.daemon = True
        self._task_handler._state = RUN
        self._task_handler.start()

        self._result_handler = threading.Thread(
            target=Pool._handle_results,
            args=(self._outqueue, self._quick_get, self._cache)
            )
        self._result_handler.daemon = True
        self._result_handler._state = RUN
        self._result_handler.start()

        self._terminate = Finalize(
            self, self._terminate_pool,
            args=(self._taskqueue, self._inqueue, self._outqueue, self._pool,
                  self._worker_handler, self._task_handler,
                  self._result_handler, self._cache),
            exitpriority=15
            )
类池(对象):
'''
类,该类支持将函数应用于参数的异步版本。
'''
过程
def uu init uuu(self,processs=None,initializer=None,initargs=()),
maxtasksperchild=None):
self.\u设置\u队列()
self.\u taskqueue=queue.queue()
self.\u cache={}
自我状态=运行
self.\u maxstasksperchild=maxstasksperchild
self.\u初始值设定项=初始值设定项
self.\u initargs=initargs
如果进程为无:
尝试:
进程=cpu_计数()
除未实施错误外:
进程=1
如果进程<1:
raise VALUERROR(“进程数必须至少为1”)
如果初始值设定项不是None且不可调用(初始值设定项):
raise TypeError('初始值设定项必须是可调用的')
self.\u进程=进程
self._pool=[]
自我重新填充池()
self.\u worker\u handler=threading.Thread(
目标=池。处理工人,
args=(self,)
)
self.\u worker\u handler.daemon=True
self.\u worker\u handler.\u state=RUN
self.\u worker\u handler.start()
self.\u task\u handler=threading.Thread(
目标=池。处理任务,
args=(self.\u taskqueue,self.\u quick\u put,self.\u outqueue,self.\u pool)
)
self.\u task\u handler.daemon=True
self.\u task\u handler.\u state=RUN
self.\u任务\u处理程序.start()
self.\u result\u handler=threading.Thread(
目标=池。\处理\结果,
args=(self.\u出列,self.\u快速获取,self.\u缓存)
)
self.\u result\u handler.daemon=True
self.\u result\u handler.\u state=RUN
self.\u result\u handler.start()
self.\u terminate=Finalize(
赛尔夫,赛尔夫,
args=(self.\u taskqueue,self.\u inqueue,self.\u outqueue,self.\u pool,
self.\u worker\u handler,self.\u task\u handler,
self.\u结果\u处理程序,self.\u缓存),
退出优先权=15
)

谢谢你的回答!我还有一个:)在我的代码中,我创建了一个池,然后我有一些逻辑将人员加载到内存中,然后我将作业扔到池中,并使用未排序的imap_对其进行迭代。从您的回答中,我看到fork发生在将staff加载到内存的逻辑之后,这是不好的,因为我将staff复制到所有worker子进程中。你知道我如何避免它(即在我开始将员工载入内存之前强制使用叉子)?谢谢@diemacht我认为应该传递
对象的参数。它应该是一个调用来初始化worker的函数,而且它似乎是执行所需操作的正确位置。