python 并行开发

四、并行开发

标签(空格分隔): 未分类


并行

串行与并行
阻塞与非阻塞任务
共享与冲突
多线程与多进程的区别和特点

多进程开发

 Linux、unix平台专属

Fork

wait

Waitpid

 
 
  1. #coding=UTF-8
  2. import os
  3. import time
  4. def myfork():
  5. a = 1
  6. pid = os.fork()
  7. if pid == 0:
  8. print "this is child %d--%d %d" %(pid, os.getpid(), os.getppid())
  9. time.sleep(1)
  10. print a+1
  11. #while True:
  12. # pass
  13. else:
  14. status = os.waitpid(pid, 0) ##将此行注释,比较执行结果
  15. #status = os.waitpid(pid. 1)
  16. print "this is parent %d--%d %d" %(pid, os.getpid(), os.getppid())
  17. print a+3
  18. if __name__ == '__main__' :
  19. myfork()

pipe和singal

进程间管道通信

 
 
  1. #coding=UTF-8
  2. import os
  3. import time
  4. def onsignal_term(a, b):
  5. print 'over'
  6. signal.signal(signal.SIGTERM,onsignal_term) #将终端信号注入到系统信号中
  7. def myfork():
  8. r, w = os.pipe()
  9. a = 1
  10. pid = os.fork()
  11. if pid == 0:
  12. os.close(r)
  13. w = os.write(w, 'w')
  14. print "this is child %d--%d %d" %(pid, os.getpid(), os.getppid())
  15. while True:
  16. a = a+1
  17. if a>100:
  18. os.kill(os.getppid, signal.SIGTERM)
  19. os.write(w,str(a))
  20. #w.write(a)
  21. print '\n'
  22. w.close()
  23. else:
  24. os.close(w)
  25. r = os.fdopen(r)
  26. print r.read()
  27. r.close()
  28. print "this is parent %d--%d %d" %(pid, os.getpid(), os.getppid())
  29. status = os.waitpid(pid, 1)
  30. if __name__ == '__main__' :
  31. myfork()

守护进程

守护进程方式

 
 
  1. #coding=UTF-8
  2. import os
  3. import sys
  4. def daemon_test():
  5. pid = os.fork()
  6. if pid > 0:
  7. sys.exit(0)
  8. os.setsid() #脱离终端
  9. os.umask(0)
  10. pid = os.fork()
  11. if pid > 0:
  12. sys.exit(0)
  13. f = open('test.txt', 'w')
  14. a =1
  15. while True:
  16. a=a+1
  17. f.write(str(a))
  18. f.close()
  19. if __name__=='__main__':
  20. daemon_test()

运行结果:

 
 
  1. ls -lh //-rw-rw-rw-. 1 root root 171M May 29 01:29 test.txt
  2. tail -f test.txt
  3. ps -ef daemon
  4. kill ***** //杀死守护进程

多线程开发

 Thread

 
 
  1. import thread
  2. import time
  3. def funt(no, a):
  4. while True:
  5. a =a+1
  6. print 'Thread no %d = %d, '%(no, a)
  7. def test():
  8. thread.start_new_thread(funt, (1,2))
  9. thread.start_new_thread(funt, (2,2))
  10. time.sleep(20) #注释看看结果,父进程会立即回收,派生的两个子线程马上就会被回收
  11. if __name__=='__main__':
  12. test()

 共享变量与临界资源:多线程操作全局变量

 
 
  1. import thread
  2. import time
  3. c=1
  4. def funt(no, a):
  5. global c
  6. while True:
  7. time.sleep(1)
  8. c =c+1
  9. print 'Thread no %d = %d, '%(no, c)
  10. def test():
  11. thread.start_new_thread(funt, (1,2))
  12. thread.start_new_thread(funt, (2,2))
  13. time.sleep(20)
  14. if __name__=='__main__':
  15. test()

 Threading 面向对象编程 
定义一个单线程类

 
 
  1. #coding=UTF-8
  2. import threading
  3. import time
  4. count = 0
  5. class aa(threading.Thread): #aa由thread对象继承而来
  6. def __init__(self, no, interval): #构造函数
  7. threading.Thread.__init__(self) #初始化基类的构造函数
  8. self.no = no
  9. self.interval = interval
  10. self.isstop = False
  11. def run(self): #重载方法
  12. global count
  13. while not self.isstop:
  14. count+=1
  15. print 'thread %d = %d' % (self.no, count)
  16. time.sleep(self.interval)
  17. def stop(self):
  18. self.isstop = True
  19. def factory():
  20. t1 = aa(1,1)
  21. t1.start()
  22. time.sleep(20)
  23. t1.stop()
  24. if __name__=="__main__":
  25. factory()

 锁机制:多线程加锁操作

 
 
  1. #coding=UTF-8
  2. import threading
  3. import time
  4. mylock = threading.RLock() #读锁
  5. count = 0
  6. class aa(threading.Thread): #aa由thread对象继承而来
  7. def __init__(self, no, interval): #构造函数
  8. threading.Thread.__init__(self) #初始化基类的构造函数
  9. self.no = no
  10. self.interval = interval
  11. self.isstop = False
  12. def run(self): #重载方法
  13. global count
  14. a =0
  15. while a<10:
  16. mylock.acquire()
  17. count+=1
  18. mylock.release()
  19. print 'thread %d = %d' % (self.no, count)
  20. #time.sleep(self.interval)
  21. a+=1
  22. def stop(self):
  23. self.isstop = True
  24. def factory():
  25. t1 = aa(1,1)
  26. t1.start()
  27. t2 = aa(2,2)
  28. t2.start()
  29. time.sleep(20)
  30. t1.stop()
  31. t2.stop()
  32. if __name__=="__main__":
  33. factory()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值