无法启动新线程(Can't start new thread)
我正在使用多个线程处理目录中的所有文件以并行处理文件。 它一切正常,除了线程似乎保持活着,因此进程的线程数上升,直到它达到1K左右的线程然后它抛出一个
thread.error can't start new thread
错误。 我知道这个错误是由线程数的操作系统级别限制引起的。 我似乎无法弄清楚错误的位置是保持线程活着。 任何想法? 这是我的代码的最小版本。class Worker(Thread): def __init__(self, tasks): Thread.__init__(self) self.tasks = tasks self.daemon = True self.start() def run(self): while True: func, args, kargs = self.tasks.get() try: func(*args, **kargs) except Exception, e: print e self.tasks.task_done() class ThreadPool: def __init__(self, num_threads): self.tasks = Queue(num_threads) for _ in range(num_threads): Worker(self.tasks) def add_task(self, func, *args, **kargs): self.tasks.put((func, args, kargs)) def wait_completion(self): self.tasks.join() def foo(filename) pool = ThreadPool(32) iterable_data = process_file(filename) for data in iterable_data: pool.add_task(some_function, data) pool.wait_completion() files = os.listdir(directory) for file in files: foo(file)
I'm processing all the files in a directory using multiple threads to process files in parallel. It all works fine, except that threads seem to stay alive and so the thread count of the process goes up until it reaches 1K or so threads and then it throws a
thread.error can't start new thread
error. I know this error is caused by an OS-level limit on thread count. I can't seem to figure out where the bug is that is keeping the threads alive. Any idea? Here is a minimal version of my code.class Worker(Thread): def __init__(self, tasks): Thread.__init__(self) self.tasks = tasks self.daemon = True self.start() def run(self): while True: func, args, kargs = self.tasks.get() try: func(*args, **kargs) except Exception, e: print e self.tasks.task_done() class ThreadPool: def __init__(self, num_threads): self.tasks = Queue(num_threads) for _ in range(num_threads): Worker(self.tasks) def add_task(self, func, *args, **kargs): self.tasks.put((func, args, kargs)) def wait_completion(self): self.tasks.join() def foo(filename) pool = ThreadPool(32) iterable_data = process_file(filename) for data in iterable_data: pool.add_task(some_function, data) pool.wait_completion() files = os.listdir(directory) for file in files: foo(file)
原文:https://stackoverflow.com/questions/16309435
最满意答案
在我们的例子中,解决方案是使数组静态:
public class ClassTest { public static bool[] abTestAvailable = new bool[_TESTS];
在创建对象之前初始化静态数组
public class ClassTest_FCT_Extern { public bool TestAvailable { get { return ClassTest.abTestAvailable[(int)ClassTest.IndividualTest.FCT_Extern]; } set { ClassTest.abTestAvailable[(int)ClassTest.IndividualTest.FCT_Extern] = value; } }
工作得很好,无一例外。 道德:尽可能保持静止!
In our case, the solution was to make the array static:
public class ClassTest { public static bool[] abTestAvailable = new bool[_TESTS];
The static array is initialized before the Objects are created
public class ClassTest_FCT_Extern { public bool TestAvailable { get { return ClassTest.abTestAvailable[(int)ClassTest.IndividualTest.FCT_Extern]; } set { ClassTest.abTestAvailable[(int)ClassTest.IndividualTest.FCT_Extern] = value; } }
works fine, without exception. Moral: make static if possible!
相关问答
更多-
由于闭包本身就是对象,因此需要将$this分配给一个局部变量,如: $host = $this; $recfunc = function($id, $name) use ($host) { ... Because closures are themselves objects, you need to assign $this to a local variable, like: $host = $this; $recfunc = function($id, $name) use ($host) { .. ...
-
让Parent类将自己的Random实例传递给Child类。 class Parent{ Child child; Random r = new Random(); public Parent() { child = new Child(r); } } class Child{ public Child(Random r){ } } 经典奥卡姆剃刀。 Have the Parent class pass its ...
-
如何在PHP中访问父对象的getter和setter?(How to access the getter and setter of a parent object in PHP?)[2022-03-02]
您需要将纯查询结果映射到doctrine实体对象。 阅读本机查询 。 $em = $this->getEntityManager(); $rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($em); $rsm->addRootEntityFromClassMetadata('my\bundle\Entity\Painting', 'p'); You need to map your plain query results t ... -
从一个对象数组中的一个对象访问父设备/ get方法(Access to a parent set/get method from one object inside an array of objects)[2024-01-18]
我想要的是b.array[0].aParentMethod返回ba , 你需要做两件事才能做到这一点: 将父对象引用传递给子对象 将吸气剂添加到使用父参考的孩子以获得a 例如,像这样的东西(见***评论): function parent() { this.a; this.array=[]; this.array.push(new child(this)); // *** } function child(parent) ... -
绕过它的唯一方法是让Parent构造函数调用Child构造函数并传入this 。 您的对象初始化程序(我假设您正在尝试这样做)然后可以在子项上设置其他属性: public class Parent { public Child Child { get; private set; } public string Name { get; set; } public Parent() { Child = new Child(this); } } publ ...
-
不。 你不能那样做。 在调试之外,需要执行此操作代表了代码中的设计问题。 你为什么需要这样做? Nope. You can't do that. Outside of debugging, needing to do this is indicative of a design problem in your code. Why do you need to do this?
-
您可以通过定义get_context_data()将任何您喜欢的数据添加到视图中。 def get_context_data(self, *args, **kwargs): context = super(ListView, self).get_context_data(*args, **kwargs): context['question'] = Question.objects.get(pk=self.kwargs['pk']) return context 或者,您可能会反映 ...
-
在我们的例子中,解决方案是使数组静态: public class ClassTest { public static bool[] abTestAvailable = new bool[_TESTS]; 在创建对象之前初始化静态数组 public class ClassTest_FCT_Extern { public bool TestAvailable { get { return ClassTest.abTestAvailable[(int)ClassTest ...
-
tl; dr: Field或Level.prototype对象中既没有field cellsObserver也没有。 在Javascript中没有经典的“自我”这样的东西。 长话故事。 当您尝试获取object.property ,Javascript将查看尝试查找property的对象,然后查看object.prototype (这是另一个对象),依此类推,直到它到达Object.prototype的null原型。参考。 所以,当你第二次调用Level构造函数中的this.cellsObserver时,它 ...
-
你有一个对象。 在实例化类时,它首先调用父类的构造函数。 对于每个父类,它遵循相同的逻辑:它首先调用父类的构造函数。 假设: A扩展B扩展C new A()导致B构造函数调用,它自己调用C构造函数。 作为C然后返回B构造函数调用, A构造函数可以继续并创建实例A 但是,我特别想知道的是它会加载类还是会创建该父类的对象。 与构造函数执行类似,在加载类之前,必须first加载其父类,并为每个父类递归加载。 这里对应于A类扩展B扩展C : class C { public C(){ ...