简体   繁体   English

在Python中限制线程

[英]Limiting threads in Python

I'm working on a project where I have a database that emits information to my Python application. 我正在一个项目中,我有一个向Python应用程序发送信息的数据库。 Whenever my Python script receives such an 'event' it has to process the data, but also be ready to receive new events. 每当我的Python脚本收到这样的“事件”时,它都必须处理数据,但也要准备接收新的事件。

When operational the script will receive lots of events in a matter of milliseconds so serial processing is not an option. 在运行时,脚本将在几毫秒内接收到许多事件,因此无法进行串行处理。

Below some pseudo code to illustrate my current setup: 下面一些伪代码来说明我当前的设置:

class Source(Thread):
# receives events and dispatches to processing threads

run():
    while True:
        data = database.receive
        for thing in data:
            DataProcessing.process(thing)

class DataProcessing():

    @Multitasking.threaded
    @staticmethod
    process(foo):
      do_something

class Multitasking():
def threaded(fn):
    def wrapper(*args, **kwargs):
        thread = Thread(target=fn, args=args, kwargs=kwargs)
        thread.setDaemon(True)
        thread.setName('worker')
        thread.start()
        return thread
    return wrapper

So here I have a Source class that acts as a listener for database events. 因此,这里有一个Source类,它充当数据库事件的侦听器。 Whenever there is an events it processes the event using the DataProcessing.process() method. 每当有事件发生时,它都会使用DataProcessing.process()方法处理该事件。 I wrote a decorator/wrapper to make it a threaded so Source can go back to listening. 我编写了一个装饰器/包装器以使其成为线程,以便Source可以返回监听状态。

Now here's my problem: I use pycharm, and discovered the concurrency diagram. 现在这是我的问题:我使用pycharm,并发现了并发图。 But when I run it something strange seems to happen. 但是当我运行它时,似乎发生了一些奇怪的事情。

concurrency_diagram Here the worker is the processing() method mentioned above. 这里的worker是上面提到的processing()方法。 As you can see the amount of active threads getting larger and larger for every event received, while I'm certain the size of the data array is not getting larger. 如您所见,对于每个接收到的事件,活动线程的数量越来越大,而我确定data数组的大小不会越来越大。

My question: How does this diagram work? 我的问题:此图如何工作? It looks like threads are re-initiated everytime a event is received, but are they? 看起来每次接收到事件时都会重新启动线程,但这是吗? I only call thread.start() for new events. 我只为新事件调用thread.start()

Thanks! 谢谢!

Not sure how this specific diagram works. 不确定此特定图表的工作方式。 But I see that you start the threads, but do not join them. 但是我看到您启动了线程,但没有加入它们。 Until joined, the threads will be treated and marked as unfinished (though not executing). 在加入之前,线程将被视为未完成(尽管未执行)并被标记为未完成。 You should do thr.join() somewhere to actually finish them and to prevent the resource leak. 您应该在某个地方执行thr.join()以实际完成它们并防止资源泄漏。 For this, you have to keep track of them, which makes to code more complex. 为此,您必须跟踪它们,这会使代码更加复杂。

Try using the undocumented multiprocessing.pool.ThreadPool , which has the same signature as from multiprocessing.Pool , but works with the threads. 尝试使用未记录的multiprocessing.pool.ThreadPool ,它具有与from multiprocessing.Pool相同的签名,但是可以与线程一起使用。 It can run a limited pool of the worker threads, and execute the tasks in them (or put the m to the queue). 它可以运行有限数量的工作线程池,并在其中执行任务(或将m放入队列)。 See: https://docs.python.org/2/library/multiprocessing.html 参见: https : //docs.python.org/2/library/multiprocessing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM