#coding: utf-8
#!/usr/bin/env python
import queue
import threading
import time
prolock = threading.lock(
)# 定義同時佇列數
queue = queue.queue(maxsize=10)
# 定義任務初值值及最大值
taskidx =
0maxidx =
100# 生成任務列表
deftasklist()
: task =
for i in
range
(100):
"task"
+str
(i))
return task
# 把任務放入佇列中
class
producer
(threading.thread)
:def
__init__
(self, name, queue)
: self.__name = name
self.__queue = queue
super
(producer, self)
.__init__(
)def
run(self)
:while
true
:global taskidx, prolock, maxidx
time.sleep(4)
prolock.acquire(
)print
'producer name: %s'
%(self.__name)
if maxidx == taskidx:
prolock.release(
)break
ips = tasklist(
) ip = ips[taskidx]
self.__queue.put(ip)
taskidx = taskidx +
1 prolock.release(
)# 執行緒處理任務
class
consumer
(threading.thread)
:def
__init__
(self, name, queue)
: self.__name = name
self.__queue = queue
super
(consumer, self)
.__init__(
)def
run(self)
:while
true
: ip = self.__queue.get(
)print
'consumer name: %s'
%(self.__name)
consumer_process(ip)
self.__queue.task_done(
)def
consumer_process
(ip)
: time.sleep(1)
print ip
defstartproducer
(thread_num)
: t_produce =
for i in
range
(thread_num)
: p = producer(
"producer"
+str
(i), queue)
p.setdaemon(
true
) p.start(
)return t_produce
defstartconsumer
(thread_num)
: t_consumer =
for i in
range
(thread_num)
: c = consumer(
"consumer"
+str
(i), queue)
c.setdaemon(
true
) c.start(
)return t_consumer
defmain()
: t_produce = startproducer(3)
t_consumer = startconsumer(5)
# 確保所有的任務都生成
for p in t_produce:
p.join(
)# 等待處理完所有任務
queue.join(
)if __name__ ==
'__main__'
: main(
)print
'------end-------'
一般生成任務都會比較快,可以使用單執行緒來生成任務,示例如下:
'''
'''#coding: utf-8
#!/usr/bin/env python
import queue
import threading
import time
# 定義同時處理任務數
queue = queue.queue(maxsize=3)
# 生成任務列表
deftasklist()
: task =
for i in
range
(100):
"task"
+str
(i))
return task
# 把任務放入佇列中
class
producer
(threading.thread)
:def
__init__
(self, name, queue)
: self.__name = name
self.__queue = queue
super
(producer, self)
.__init__(
)def
run(self)
:for ip in tasklist():
self.__queue.put(ip)
# 執行緒處理任務
class
consumer
(threading.thread)
:def
__init__
(self, name, queue)
: self.__name = name
self.__queue = queue
super
(consumer, self)
.__init__(
)def
run(self)
:while
true
: ip = self.__queue.get(
)print
'consumer name: %s'
%(self.__name)
consumer_process(ip)
self.__queue.task_done(
)def
consumer_process
(ip)
: time.sleep(1)
print ip
defstartconsumer
(thread_num)
: t_consumer =
for i in
range
(thread_num)
: c = consumer(i, queue)
c.setdaemon(
true
) c.start(
)return t_consumer
defmain()
: p = producer(
"producer task0"
, queue)
p.setdaemon(
true
) p.start(
) startconsumer(9)
# 確保所有的任務都生成
p.join(
)# 等待處理完所有任務
queue.join(
)if __name__ ==
'__main__'
: main(
)print
'------end-------'
python多執行緒限制併發數示例
coding utf 8 usr bin env python import queue import threading import time prolock threading.lock 定義同時佇列數 queue queue.queue maxsize 10 定義任務初值值及最大值 task...
C 多執行緒 限制執行緒數量
這裡需要用到互斥鎖mutex,條件變數condition variable,通用互斥鎖包裝器unique lock std unique lock也可以提供自動加鎖 解鎖功能,比std lock guard更加靈活。類 unique lock 是通用互斥包裝器,允許延遲鎖定 鎖定的有時限嘗試 遞迴鎖...
多執行緒執行(限制執行緒數)
多執行緒執行 限制執行緒數 coding utf 8 import threading from ctypes import import queue,os import time,datetime class store threading.thread def init self,store,q...