for i in range(5): print(threading.current_thread().name+' main ', i) print(thread.name+' is alive ', thread.isAlive()) # 判断线程是狗准确 time.sleep(1)
结果
TestThread test 0 TestThread test 1 TestThread test 2 TestThread test 3 TestThread test 4 MainThread main 0 TestThread is alive False MainThread main 1 TestThread is alive False MainThread main 2 TestThread is alive False MainThread main 3 TestThread is alive False MainThread main 4 TestThread is alive False
如果我门想要主进程结束的时候,子进程也要跟着结果,要怎么做呢,这里就可以用到daemon了。
我们可以设置主线程的执行时间比子线程慢,来检测一下
#-*-coding:utf-8-*- import threading import time
def test():
for i in range(10): print(threading.current_thread().name+' test ',i) time.sleep(1)
for i in range(10): print(threading.current_thread().name+' main ', i) print(thread.name+' is alive ', thread.isAlive()) time.sleep(0.5)
结果:
MainThread main 0 TestThread is alive True TestThread test 0 MainThread main 1 TestThread is alive True TestThread test 1 MainThread main 2 TestThread is alive True MainThread main 3 TestThread is alive True TestThread test 2 MainThread main 4 TestThread is alive True MainThread main 5 TestThread is alive True TestThread test 3 MainThread main 6 TestThread is alive True MainThread main 7 TestThread is alive True TestThread test 4 MainThread main 8 TestThread is alive True MainThread main 9 TestThread is alive True
window1 notice:There has 10 tickts remain window1 have buy 1 tickt,the remain tickt't count is 9 .Already buy 1 window1 notice:There has 9 tickts remain window1 have buy 1 tickt,the remain tickt't count is 8 .Already buy 2
window2 notice:There has 8 tickts remain window2 have buy 2 tickt,the remain tickt't count is 6 .Already buy 2 window1 notice:There has 6 tickts remain window2 notice:There has 6 tickts remain window2 have buy 1 tickt,the remain tickt't count is 5 .Already buy 3
window1 have buy 2 tickt,the remain tickt't count is 3 .Already buy 4 window1 notice:There has 3 tickts remain window1 have buy 1 tickt,the remain tickt't count is 2 .Already buy 5 window2 notice:There has 2 tickts remain window2 have buy 1 tickt,the remain tickt't count is 1 .Already buy 4 window2 notice:There has 1 tickts remain window3 notice:There has 1 tickts remain window3 have buy 1 tickt,the remain tickt't count is 0 .Already buy 1
window1 notice:There is no tickt can sold! Already sold 5
window2 have buy 1 tickt,the remain tickt't count is -1 .Already buy 5 window3 notice:There is no tickt can sold! Already sold 1 window2 notice:There is no tickt can sold! Already sold 5 tickt count -1
我们惊奇的发现居然会有 -1出现,这就是数据共享出现了错误,如果我们加上锁:
如下:(自行添加测试噢)
self.lock.acquire() if tickt_count > 0: if tickt_count > 2: number = random.randint(1,2) else: number = 1 tickt_count -= number self.tickts += number
print('%s have buy %d tickt,the remain tickt\'t count is %d .Already buy %d \n' % (self.name, number, tickt_count, self.tickts)) self.lock.release()
实例:(Windows下的Process()必须放在 if _name_ == '_main_' )下运行
from multiprocessing import Process import os
def run_proc(name): print('Run child process {}({})'.format(name, os.getpid()))
if __name__ == '__main__': print('Parent process {}'.format(os.getpid())) p = Process(target=run_proc, args=('test',)) print('Child process will start') p.start() p.join() print("Child process end") ----------------------------------------------------------------------------- Parent process 27808 Child process will start Run child process test(28372) Child process end
if __name__ == '__main__': lists = range(100) pool = Pool(10) pool.map(test, lists) pool.close() pool.join()
# 异步进程池 #-*-coding:utf-8-*- from multiprocessing import Pool
def test(i): print(i)
if __name__ == '__main__': pool = Pool(8) for i in range(100): ''' For循环中执行步骤: (1)循环遍历,将100个子进程添加到进程池(相对父进程会阻塞) (2)每次执行8个子进程,等一个子进程执行完后,立马启动新的子进程。(相对父进程不阻塞) apply_async为异步进程池写法。异步指的是启动子进程的过程,与父进程本身的执行(print)是异步的,而For循环中往进程池添加子进程的过程,与父进程本身的执行却是同步的。 ''' pool.apply_async(test, args=(i,)) print("test") pool.close() pool.join()
#-*-coding:utf-8-*- from multiprocessing import Process, Queue import os, time, random
def write(q): print('Process to write {}'.format(os.getpid())) for value in ['A', 'B', 'C']: print('Put {} to queue'.format(value)) q.put(value) time.sleep(random.random())
def read(q): print('Process to read: {}'.format(os.getpid())) while True: value = q.get(True) print('Get {} from queue'.format(value))