标签:
线程
调用默认的多线程
-----------------------------------------------------------------------
import threading
def f1(arg):
print(arg)
t = threading.Thread(target=f1,args=(123,))
t.start()
自定义多线程
-----------------------------------------------------------------------
import threading
class MyThread(threading.Thread):
def __init__(self,func,args):
self.func = func
self.args = args
super(MyThread,self).__init__()
def run(self):
self.func(self.args)
def f2(arg):
print(arg)
obj = MyThread(f2,123)
obj.start()
消息队列
# queue.Queue,先进先出队列
# queue.LifoQueue,后进先出队列
# queue.PriorityQueue,优先级队列
# queue.deque,双向对队
# queue.Queue(2) 先进先出队列
# put放数据,是否阻塞,阻塞时的超时时间
# get取数据(默认阻塞),是否阻塞,阻塞时的超时时间
# qsize()真实个数
# maxsize 最大支持的个数
# join,task_done,阻塞进程,当队列中任务执行完毕之后,不再阻塞
import queue
q = queue.Queue(2)
print(q.empty())
q.put(11)
q.put(22)
print(q.empty())
print(q.qsize())
q.put(22)
q.put(33,block=False)
q.put(33,block=False,timeout=2)
print(q.get())
print(q.get())
print(q.get(timeout=2))
import queue
q = queue.Queue(5)
q.put(123)
q.put(456)
q.get()
q.task_done() #读完成
q.get()
q.task_done()
q.join()
先进先出队列
--------------------------------------------------------------------------------
import queue
q = queue.Queue(10) #10的意思,队列最多接收10个队列,参数可以自定义
q.put(11) #向队列里传数据
q.put(22) #向队列里传数据
q.put(33) #向队列里传数据
print(q.qsize()) #输出当前队列里有多少数据
print(q.get()) #从队列里取数据
输出结果
3
11
import queue
queue.Queue #先进先出队列
queue.LifoQueue #后队进先出队列
queue.PriorityQueue #优先级队列
queue.deque #双向对队
q = queue.LifoQueue()
q.put(123)
q.put(456)
print(q.get())
q = queue.PriorityQueue()
q.put((1,"alex1"))
q.put((1,"alex2"))
q.put((1,"alex3"))
q.put((3,"alex3"))
print(q.get())
q = queue.deque()
q.append(123)
q.append(333)
q.appendleft(456)
q.pop()
q.popleft()
队列与线程结合
线程锁
----------------------------------------------------------------------------
import threading
import time
NUM = 10
def func(l):
global NUM
# 上锁
l.acquire()
NUM -= 1
time.sleep(2)
print(NUM)
# 开锁
l.release()
#lock = threading.Lock()
lock = threading.RLock()
for i in range(10):
t = threading.Thread(target=func,args=(lock,))
t.start()
信号量
----------------------------------------------------------------------------
import threading
import time
NUM = 10
def func(i,l):
global NUM
# 上锁
l.acquire()
NUM -= 1
time.sleep(2)
print(NUM,i)
# 开锁
l.release()
#lock = threading.Lock()
#lock = threading.RLock()
lock = threading.BoundedSemaphore(5)
for i in range(10):
t = threading.Thread(target=func,args=(i,lock,))
t.start()
全锁
------------------------------------------------------------------------------
import threading
def func(i,e):
pass
e.wait() #检测是什么灯,如果是红灯,停:绿,行
print(i+100)
event = threading.Event()
for i in range(10):
t = threading.Thread(target=func,args=(i,event,))
t.start()
event.clear() #设置成红灯
inp = input(‘>>>‘)
if inp == ‘1‘:
event.set() #设置成绿灯
http://www.cnblogs.com/wupeiqi/articles/5132791.html
条件
------------------------------------------------------------------------------
import threading
def condition():
ret = False
r = input(‘>>>‘)
if r == ‘true‘:
ret = True
else:
ret = False
return ret
def func(i,con):
print(i)
con.acquire()
con.wait_for(condition)
print(i+100)
con.release()
c = threading.Condition()
for i in range(10):
t = threading.Thread(target=func, args=(i,c,))
t.start()
------------------------------------------------------------------------------
import threading
def func(i,con):
print(i)
con.acquire()
con.wait()
print(i+100)
con.release()
c = threading.Condition()
for i in range(10):
t = threading.Thread(target=func, args=(i,c,))
t.start()
while True:
inp = input(‘>>>‘)
if inp == ‘q‘:
break
c.acquire()
c.notify(int(inp))
c.release()
from threading import Timer
------------------------------------------------------------------------------
def hello():
print("hello, world")
t = Timer(1, hello)
t.start() # after 1 seconds, "hello, world" will be printed
#线程池
import queue
import threading
import time
class ThreadPool:
def __init__(self,maxsize=5):
self.maxsize = maxsize
self._q = queue.Queue(maxsize)
for i in range(maxsize):
self._q.put(threading.Thread)
def get_thread(self):
return self._q.get()
def add_thread(self):
self._q.put(threading.Thread)
pool = ThreadPool(5)
def task(arg,p):
print(arg)
time.sleep(1)
p.add_thread()
for i in range(100):
t = pool.get_thread()
obj = t(target=task,args=(i,pool,))
obj.start()
#串行没有多线程
from multiprocessing import Pool
import time
def f1(arg):
time.sleep(1)
print(arg)
if __name__ == "__main__":
pool = Pool(5)
for i in range(30):
pool.apply(func=f1,args=(i,))
print(‘end‘)
# 五个一起执行
from multiprocessing import Pool
import time
def f1(arg):
time.sleep(1)
print(arg)
if __name__ == "__main__":
pool = Pool(5)
for i in range(30):
#pool.apply(func=f1,args=(i,))
pool.apply_async(func=f1,args=(i,)) #去队列中取任务
pool.close()
pool.join()
#执行一部份
from multiprocessing import Pool
import time
def f1(arg):
time.sleep(1)
print(arg)
if __name__ == "__main__":
pool = Pool(5)
for i in range(30):
#pool.apply(func=f1,args=(i,))
pool.apply_async(func=f1,args=(i,)) #去队列中取任务
time.sleep(2)
# pool.close() #所有的任务执行完毕
pool.terminate() #立即终止
pool.join()
标签:
原文地址:http://www.cnblogs.com/wml1989/p/5689226.html