ThreadPool:错误:无法启动新线程

时间:2018-10-17 10:51:32

标签: multithreading python-2.7 threadpool

我有一棵树,有许多节点(大约20-25)。 我使用ThreadPool以BFS方式执行所有节点的运行方法,即不是立即调用所有节点,而是树中处于该深度级别的节点。

我最多有10棵这样的树,它们全部同时启动以使用线程执行下面的run方法。我将ThreadPool与4个线程一起使用。这意味着我的进程在每个点上都有大约10 * 4 + 20(我可能正在启动的其他线程)被执行的线程,大致来说。无论哪种方式都不能接近数千。

但是我仍然不时得到:

pool = ThreadPool(self.number_of_threads) 
File "/usr/lib64/python2.7/multiprocessing/pool.py", line 732, in __init__ 
Pool.__init__(self, processes, initializer, initargs) 
File "/usr/lib64/python2.7/multiprocessing/pool.py", line 161, in __init__ 
self._repopulate_pool() 
File "/usr/lib64/python2.7/multiprocessing/pool.py", line 225, in _repopulate_pool 
w.start() 
File "/usr/lib64/python2.7/multiprocessing/dummy/__init__.py", line 75, in start 
threading.Thread.start(self) 
File "/usr/lib64/python2.7/threading.py", line 739, in start 
_start_new_thread(self.__bootstrap, ()) 
error: can't start new thread

这是我的运行方法:

def run(self):
    """
    Run all TreeNode's of the this ThreadedTree using ThreadPool.
    Iterate over all TreeNode's of this ThreadedTree level by level and invoke the run method.
    :return: None.
    """
    nodes = self.roots
    while nodes:
        pool = ThreadPool(4)
        rc = sum(pool.map(self.__run_node, [node.name for node in nodes]))
        _nodes = []
        for node in nodes:
            _nodes.extend(node.get_children())
        nodes = _nodes

通过研究此问题,我了解到我可能会遇到某种python进程限制,即我可以打开多少个线程。但是,如果它与操作系统的限制没有区别(在我的情况下,我在Fedora27上工作,并拥有95648),那么我不明白如何才能达到该限制。

0 个答案:

没有答案
相关问题