Python线程:只有两个活动线程,我如何获得更多? [英] Python Threading: Only two active threads, how do I get more?

查看:106
本文介绍了Python线程:只有两个活动线程,我如何获得更多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有以下程序:

https://github.com/eWizardII/homobabel/blob/master/Experimental/demo_async_falcon.py

但是,当它运行时,我只有两个运行中的线程正在运行,我该如何使它运行,以便有更多的线程在运行.我尝试做类似urlv2 = birdofprey(ip2)的操作,其中ip2 = str(host+1)只是将相同的内容发送到两个线程.任何帮助将不胜感激.

However, when it's run I only get two active threads are running, how can I make it so that there are more threads running. I have tried doing stuff like urlv2 = birdofprey(ip2) where ip2 = str(host+1) however that just ends up sending the same thing to two threads. Any help would be appreciated.

谢谢

推荐答案

活动计数= 2意味着您拥有设计线程(birdofprey)和主线程之一.这是因为使用锁,所以第二个birdofprey线程等待第一个,依此类推.我没有更深入地了解算法,但是似乎您不需要锁定birdofprey线程,因为它们不共享任何数据(我可能会弄错).如果它们共享,则您应独占访问共享数据,而不要锁定run的整个主体.

Active count=2 means that you have one of your designed thread (birdofprey), and the main thread. This is because you use lock, so the second birdofprey thread waits for the first and so on. I didn't get deeper into the algorithm, but it seems that you don't need to lock birdofprey threads, since they don't share any data (I could get wrong). If they share, you should make exclusive access to the shared data, and not to lock the whole body of run.

根据评论更新

  1. 删除锁(如果没有共享数据.storage_i不是共享数据.);
  2. for循环中创建线程,启动线程,追加到列表中;
  3. 在线程列表上进行第二次循环,调用join收集所需的信息.
  1. remove locks (if there is no shared data. storage_i is not a shared data.);
  2. in the for loop` create threads, start them, append to a list;
  3. make the second loop over the list of threads, call join collect the information you need.

这篇关于Python线程:只有两个活动线程,我如何获得更多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆