指定Python线程数目

时间:2023-03-09 19:19:05
指定Python线程数目

  可以通过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