【问题标题】:Python and Threads - Threads Slowly die if running more than one processPython 和线程 - 如果运行多个进程,线程会慢慢死亡
【发布时间】:2012-02-23 19:55:16
【问题描述】:

我正在尝试执行以下操作。

  1. 我有 8 个内核。

  2. 我执行 8 个进程如下,其中 core_aa 是将 url 加载到队列中的文件名

    python threaded_crawl.py core_aa --max_async_count=20 --use_headers --verbose > /tmp/core_aa.out
    python threaded_crawl.py core_ab --max_async_count=20 --use_headers --verbose > /tmp/core_ab.out
    python threaded_crawl.py core_ac --max_async_count=20 --use_headers --verbose > /tmp/core_ac.out
    python threaded_crawl.py core_ad --max_async_count=20 --use_headers --verbose > /tmp/core_ad.out
    python threaded_crawl.py core_ae --max_async_count=20 --use_headers --verbose > /tmp/core_ae.out
    python threaded_crawl.py core_af --max_async_count=20 --use_headers --verbose > /tmp/core_af.out
    python threaded_crawl.py core_ag --max_async_count=20 --use_headers --verbose > /tmp/core_ag.out
    python threaded_crawl.py core_ah --max_async_count=20 --use_headers --verbose > /tmp/core_ah.out
    
  3. 如果进程是运行 20 个线程的线程应用程序,每个线程的任务是获取 url。如果我有例如60K url,我运行一个进程,作业完成,所有线程都存活到队列为空

  4. 如果我运行多个进程,我注意到线程开始缓慢死亡,例如每 1000 人死亡。idea os 将一个进程的 60K 拆分为 8。线程总数为 20*8

  5. 每个进程不共享数据。

既然一项工作是一项工作,为什么执行多个进程会杀死线程?

我该如何解决?

class ThreadClass(threading.Thread):
def __init__(self,parms={},proxy_list=[],user_agent_list=[],use_cookies=True,fn=None,verbose=False):
        threading.Thread.__init__(self)
 def run(self):
    while page_queue.qsize()>0:
         FETCH URLS....


for page in xrange(THREAD_LIMIT):
        tc = ThreadClass(parms=parms,proxy_list=proxy_list,user_agent_list=user_agent_list,use_cookies=use_cookies,fn=fn,verbose=verbose)
        tc.start()
        while threading.activeCount()>=THREAD_LIMIT:
            time.sleep(1)
        while threading.activeCount()>1:
                time.sleep(1)

我知道如何调试并且没有错误。鉴于我有以下情况,

while threading.activeCount()>1:
                time.sleep(1)

一旦线程都死了,代码会继续,即使队列中还有剩余的项目,线程应该运行直到队列为空。

很困惑。

一旦活跃计数

【问题讨论】:

  • PS..对于我使用的每个线程 opener = urllib2.build_opener()。有关于文件描述符 c.f. 的讨论。 stackoverflow.com/questions/9308166/in-python-when-threads-die。这可能是原因吗?我没有打开文件,但我正在使用 urllib2 来获取网页。
  • PPS 当我添加以下代码以启动新线程时,如果最大线程数低于其工作的阈值 --> 而 threading.activeCount()>1: if threading.activeCount()THREAD_LIMIT: ntc = THREAD_LIMIT - threading.activeCount() for i in xrange(ntc): tc = ThreadClass(parms=parms,proxy_list=proxy_list,user_agent_list=user_agent_list,use_cookies=use_cookies,fn=fn, verbose=verbose) tc.start() print "started a new thread" time.sleep(1)
  • 如果您愿意,可以edit您的问题并添加更多信息,这将比评论更清楚。

标签: python multithreading


【解决方案1】:

.qsize() 返回一个近似大小。不要使用page_queue.qsize() > 0 来检查队列是否为空。您可以使用while True: .. page_queue.get() .. 和哨兵来知道您何时完成,examplequeue.task_done()queue.join() 组合。

.run() 方法中捕获异常以避免过早杀死线程。

如果您需要n 线程,请不要使用.activeCount(),然后只需创建n 线程即可。

使您的线程成为守护进程,以便能够随时中断您的程序。

如果您的程序受 IO 限制,则您不需要多个进程。否则,您可以使用multiprocessing 模块来管理多个进程,而不是手动启动它们。

【讨论】:

    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多