threading使用 (工业风案例)
import threading
from time import sleep, ctime
loop = [4, 2]
class ThreadFunc:
def __init__(self, name):
self.name = name
def loop(self, nloop, nsec):
'''
:param nloop: loop函数的名称
:param nsec: 系统休眠时间
:return:
'''
print('Start loop ', nloop, 'at ', ctime())
sleep(nsec)
print('Done loop ', nloop, ' at ', ctime())
def main():
print("Starting at: ", ctime())
# ThreadFunc("loop").loop 跟一下两个式子相等:
# t = ThreadFunc("loop")
# t.loop
# 以下t1 和 t2的定义方式相等
t = ThreadFunc("loop")
t1 = threading.Thread( target = t.loop, args=("LOOP1", 4))
# 下面这种写法更西方人,工业化一点
t2 = threading.Thread( target = ThreadFunc('loop').loop, args=("LOOP2", 2))
# 常见错误写法
#t1 = threading.Thread(target=ThreadFunc('loop').loop(100,4))
#t2 = threading.Thread(target=ThreadFunc('loop').loop(100,2))
t1.start()
t2.start()
t1.join( )
t2.join()
print("ALL done at: ", ctime())
if __name__ == '__main__':
main()
输出>>>
Starting at: Sun Sep 2 10:04:47 2018
Start loop LOOP1 at Sun Sep 2 10:04:47 2018
Start loop LOOP2 at Sun Sep 2 10:04:47 2018
Done loop LOOP2 at Sun Sep 2 10:04:49 2018
Done loop LOOP1 at Sun Sep 2 10:04:51 2018
ALL done at: Sun Sep 2 10:04:51 2018
分析:注意:实例化时threading.Thread(target=xxx, args=(xxx,))格式完整,工业风写法为init了的类的函数,args为其余param,一行搞定喵
-
可以通过设置守护线程,使不重要线程同主线程一同结束
t1 = threading.Thread(target=fun, args=() ) # 社会守护线程的方法,必须在start之前设置,否则无效 t1.setDaemon(True) #t1.daemon = True t1.start()
threading.Lock()
的两个线程,竞争资源都acquire()
,造成无法release()
,最后无法继续程序。threading.Semaphore(n)
n=允许同时运行线程数threading.Timer(t, func)
指定时间开始线程
多进程
用
multiprocessing.Process()
直接生成进程-
创建子类生成:
import multiprocessing from time import sleep, ctime class ClockProcess(multiprocessing.Process): ''' 两个函数比较重要 1. init构造函数 2. run ''' def __init__(self, interval): super().__init__() self.interval = interval def run(self): while True: print("The time is %s" % ctime()) sleep(self.interval) if __name__ == '__main__': p = ClockProcess(3) p.start() while True: print('sleeping.......') sleep(1)
注意:
__init__
里用super().__init__
重写
run()
可以用
os.getppid()
得到父进程id,用os.getpid()
得到本进程id-
建立进程:
q = multiprocessing.JoinableQueue() # 运行消费者进程 cons_p = multiprocessing.Process (target = consumer, args = (q,)) cons_p.daemon = True cons_p.start()
example:
import multiprocessing
from time import ctime
def consumer(input_q):
print("Into consumer:", ctime())
while True:
item = input_q.get()
if item is None:
break
print("pull", item, "out of q")
print("Out of consumer:", ctime())
def producer(sequence, output_q):
for item in sequence:
print("Into procuder:", ctime())
output_q.put(item)
print("Out of procuder:", ctime())
if __name__ == '__main__':
q = multiprocessing.Queue()
cons_p1 = multiprocessing.Process(target=consumer, args=(q,))
cons_p1.start()
cons_p2 = multiprocessing.Process (target=consumer, args=(q,))
cons_p2.start()
sequence = [1, 2, 3, 4]
producer(sequence, q)
q.put(None)
q.put(None)
cons_p1.join()
cons_p2.join()
Into procuder: Tue Sep 4 15:57:37 2018
Out of procuder: Tue Sep 4 15:57:37 2018
Into procuder: Tue Sep 4 15:57:37 2018
Out of procuder: Tue Sep 4 15:57:37 2018
Into procuder: Tue Sep 4 15:57:37 2018
Out of procuder: Tue Sep 4 15:57:37 2018
Into procuder: Tue Sep 4 15:57:37 2018
Out of procuder: Tue Sep 4 15:57:37 2018
Into consumer: Tue Sep 4 15:57:37 2018
pull 1 out of q
pull 2 out of q
pull 3 out of q
pull 4 out of q
Out of consumer: Tue Sep 4 15:57:37 2018
Into consumer: Tue Sep 4 15:57:37 2018
Out of consumer: Tue Sep 4 15:57:37 2018
分析:
-
multiprocessing.Queue()
建立一个进程间队列 -
Queue.put()
以及Queue.get()
为队列操作,先进先出