可以通过threading.semaphore()来指定并行运行的Python线程的数目。

#!/usr/bin/python2.7
#File: threadsNum.py
#Author: lxw
#Time: 2014-09-07
#Usage: Demonstration for control the number of threads. import threading
from myThread import MyThread
from time import sleep def fib(x):
sleep(0.005)
if x < 2:
return 1
return fib(x-2) + fib(x-1) argList = [13, 11, 15, 12, 14] def main():
#Limit the number of threads to 3.
threadingNum = threading.Semaphore(3)
threads = []
for arg in argList:
t = MyThread(fib, (arg,), threadingNum, fib.__name__+str(arg))
threads.append(t) #There are 5 threads in all, but at most 3 of them run at the same time.
for thread in threads:
thread.start() for thread in threads:
#if t is threading.currentThread():
# continue
thread.join()
print(thread.getResult()) if __name__ == '__main__':
main()
else:
print('Being imported as a module.')

其中用到的myThread.py如下:

#!/usr/bin/python2.7
#File: myThread.py
#Author: lxw
#Time: 2014-09-06 import threading
from time import ctime class MyThread(threading.Thread):
def __init__(self, func, args, num, name=""):
#if the subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.
threading.Thread.__init__(self)
self.func = func
self.args = args
self.threadingNum = num
self.name = name def getResult(self):
return self.res def run(self):
#NOTE: "with".
with self.threadingNum:
print("start {0} at: {1}".format(self.name, ctime()))
self.res = apply(self.func, self.args)
print("end {0} at: {1}".format(self.name, ctime()))

Output:

lxw@lxw-PC:TASK2$ python threadsNum.py
start fib13 at: Sun Sep ::
start fib11 at: Sun Sep ::
start fib15 at: Sun Sep ::
end fib11 at: Sun Sep ::
start fib12 at: Sun Sep ::
end fib12 at: Sun Sep ::
start fib14 at: Sun Sep ::
end fib13 at: Sun Sep :: end fib14 at: Sun Sep ::
end fib15 at: Sun Sep ::

通过下面的输出结果我们能够更好地了解(最多允许5个线程同时运行):

lxw GetIPMultiThread$ python getIP.py
start at: Wed Mar ::
start at: Wed Mar ::
start at: Wed Mar ::
start at: Wed Mar ::
start at: Wed Mar ::
end at: Wed Mar ::
start at: Wed Mar ::
end at: Wed Mar ::
start at: Wed Mar ::
end at: Wed Mar ::
start at: Wed Mar ::
end at: Wed Mar ::
start at: Wed Mar ::
end at: Wed Mar ::
start at: Wed Mar ::
end at: Wed Mar ::
end at: Wed Mar ::
end at: Wed Mar ::
end at: Wed Mar ::

Reference:

Python继承类的方式实现多线程及控制线程数: http://lihuipeng.blog.51cto.com/3064864/1322247

指定Python线程数目的更多相关文章

  1. Python线程和协程-day10

    写在前面 上课第10天,打卡: 感谢Egon老师细致入微的讲解,的确有学到东西! 一.线程 1.关于线程的补充 线程:就是一条流水线的执行过程,一条流水线必须属于一个车间: 那这个车间的运行过程就是一 ...

  2. Python线程和协程

    写在前面 好好学习 天天向上 一.线程 1.关于线程的补充 线程:就是一条流水线的执行过程,一条流水线必须属于一个车间: 那这个车间的运行过程就是一个进程: 即一个进程内,至少有一个线程: 进程是一个 ...

  3. Python 线程池模块threadpool 、 concurrent.futures 的 ThreadPoolExecutor

    一.threadpool   基本用法 pip install threadpool pool = ThreadPool(poolsize) requests = makeRequests(some_ ...

  4. [python] 线程池

    特别感谢simomo 什么是线程池? 诸如web服务器.数据库服务器.文件服务器和邮件服务器等许多服务器应用都面向处理来自某些远程来源的大量短小的任务.构建服务器应用程序的一个过于简单的模型是:每当一 ...

  5. [python] 线程简介

    参考:http://www.cnblogs.com/aylin/p/5601969.html 我是搬运工,特别感谢张岩林老师! python 线程与进程简介 进程与线程的历史 我们都知道计算机是由硬件 ...

  6. python线程使用场景 多线程下载

    http://blog.xiayf.cn/2015/09/11/parallelism-in-one-line http://python.jobbole.com/84327/ http://www. ...

  7. Semaphore (通常用于限制可以访问某些资源(物理或逻辑的)的线程数目)

    Semaphore 通常用于限制可以访问某些资源(物理或逻辑的)的线程数目.例如,下面的类使用信号量控制对内容池的访问: 方法详解: 构造方法摘要 Semaphore(int permits)     ...

  8. python 线程(一)理论部分

    Python线程 进程有很多优点,它提供了多道编程,可以提高计算机CPU的利用率.既然进程这么优秀,为什么还要线程呢?其实,仔细观察就会发现进程还是有很多缺陷的. 主要体现在一下几个方面: 进程只能在 ...

  9. python线程同步原语--源码阅读

    前面两篇文章,写了python线程同步原语的基本应用.下面这篇文章主要是通过阅读源码来了解这几个类的内部原理和是怎么协同一起工作来实现python多线程的. 相关文章链接:python同步原语--线程 ...

随机推荐

  1. Atitit.互联网 软件编程 数据库方面 架构 大牛 牛人 attilax总结

    Atitit.互联网 软件编程 数据库方面 架构 大牛 牛人 attilax总结 Coolshell 称号.理论与c++ 阮一峰:: 理论高手与js高手 王银:理论高手 赵劼,网名老赵,c#高手 与理 ...

  2. C#实现插件的“动态替换”

    如果某个"功能"需要动态更新?这种动态更新,可能是需求驱动的,也可能是为了修改 BUG,面对这种场景,如何实现“热插拔”呢?先解释一下“热插拔”:在系统运行过程动态替换某些功能,不 ...

  3. Quarta介绍

    环境:XP+Myeclipse6.5+JDK1.6 quartz官网:http://www.quartz-scheduler.org/ 参考资料 1 Quartz任务调度快速入门 http://www ...

  4. MySQL和hive对比表结构脚本

    #!/bin/bash source /etc/profile runlog='/tmp/zewei/check_schema_log' hive_database_schema=/tmp/hive_ ...

  5. 怎么在Word中找MathType菜单

    一些用户朋友在使用word的过程中,发现自己突然找不到MathType公式编辑器菜单项了,而这个时候又急着编写公式,所以会特别的着急.下面我们就来针对这个问题好好的给大家分析一下,并提供解决方案.请关 ...

  6. Error:“const char*”类型的实参与“wchar_t”类型的形参不兼容

    MainApp\RPolarView.cpp(1571): error C2664: “ATL::CStringT<BaseType,StringTraits>::ReverseFind” ...

  7. ios -逆向-代码混淆

    该方法只能针对有.m.h的类进行混淆,静态库等只有.h文件的没法进行混淆 代码混淆,刚刚看到是不是有点懵逼,反正我是最近才接触到这么个东西,因为之前对于代码和APP,只需要实现功能就好了,根本没有考虑 ...

  8. Codeforces Beta Round #25 (Div. 2)--A. IQ test

    IQ test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  9. 《从零开始学Swift》学习笔记(Day 44)——重写属性

    原创文章,欢迎转载.转载请注明:关东升的博客 重写实例属性 我们可以在子类中重写从父类继承来的属性,属性有实例属性和静态属性之分,他们在具体实现也是不同的. 实例属性的重写一方面可以重写getter和 ...

  10. Python学习笔记(三)windows下安装theano

    2016.6.28补充: 不论是实验室的电脑还是我的笔记本,只要是windows下,theano.test()都是不通过的.虽然能使用一些theano中的函数,但是我感觉很不好. 所以还是转Ubunt ...