四、并行开发
标签(空格分隔): 未分类
并行
串行与并行
阻塞与非阻塞任务
共享与冲突
多线程与多进程的区别和特点
多进程开发
Linux、unix平台专属
Fork
wait
Waitpid
#coding=UTF-8import osimport timedef myfork():a = 1pid = os.fork()if pid == 0:print "this is child %d--%d %d" %(pid, os.getpid(), os.getppid())time.sleep(1)print a+1#while True:# passelse:status = os.waitpid(pid, 0) ##将此行注释,比较执行结果#status = os.waitpid(pid. 1)print "this is parent %d--%d %d" %(pid, os.getpid(), os.getppid())print a+3if __name__ == '__main__' :myfork()
pipe和singal
进程间管道通信
#coding=UTF-8import osimport timedef onsignal_term(a, b):print 'over'signal.signal(signal.SIGTERM,onsignal_term) #将终端信号注入到系统信号中def myfork():r, w = os.pipe()a = 1pid = os.fork()if pid == 0:os.close(r)w = os.write(w, 'w')print "this is child %d--%d %d" %(pid, os.getpid(), os.getppid())while True:a = a+1if a>100:os.kill(os.getppid, signal.SIGTERM)os.write(w,str(a))#w.write(a)print '\n'w.close()else:os.close(w)r = os.fdopen(r)print r.read()r.close()print "this is parent %d--%d %d" %(pid, os.getpid(), os.getppid())status = os.waitpid(pid, 1)if __name__ == '__main__' :myfork()
守护进程
守护进程方式
#coding=UTF-8import osimport sysdef daemon_test():pid = os.fork()if pid > 0:sys.exit(0)os.setsid() #脱离终端os.umask(0)pid = os.fork()if pid > 0:sys.exit(0)f = open('test.txt', 'w')a =1while True:a=a+1f.write(str(a))f.close()if __name__=='__main__':daemon_test()
运行结果:
ls -lh //-rw-rw-rw-. 1 root root 171M May 29 01:29 test.txttail -f test.txtps -ef daemonkill ***** //杀死守护进程
多线程开发
Thread
import threadimport timedef funt(no, a):while True:a =a+1print 'Thread no %d = %d, '%(no, a)def test():thread.start_new_thread(funt, (1,2))thread.start_new_thread(funt, (2,2))time.sleep(20) #注释看看结果,父进程会立即回收,派生的两个子线程马上就会被回收if __name__=='__main__':test()
共享变量与临界资源:多线程操作全局变量
import threadimport timec=1def funt(no, a):global cwhile True:time.sleep(1)c =c+1print 'Thread no %d = %d, '%(no, c)def test():thread.start_new_thread(funt, (1,2))thread.start_new_thread(funt, (2,2))time.sleep(20)if __name__=='__main__':test()
Threading 面向对象编程
定义一个单线程类
#coding=UTF-8import threadingimport timecount = 0class aa(threading.Thread): #aa由thread对象继承而来def __init__(self, no, interval): #构造函数threading.Thread.__init__(self) #初始化基类的构造函数self.no = noself.interval = intervalself.isstop = Falsedef run(self): #重载方法global countwhile not self.isstop:count+=1print 'thread %d = %d' % (self.no, count)time.sleep(self.interval)def stop(self):self.isstop = Truedef factory():t1 = aa(1,1)t1.start()time.sleep(20)t1.stop()if __name__=="__main__":factory()
锁机制:多线程加锁操作
#coding=UTF-8import threadingimport timemylock = threading.RLock() #读锁count = 0class aa(threading.Thread): #aa由thread对象继承而来def __init__(self, no, interval): #构造函数threading.Thread.__init__(self) #初始化基类的构造函数self.no = noself.interval = intervalself.isstop = Falsedef run(self): #重载方法global counta =0while a<10:mylock.acquire()count+=1mylock.release()print 'thread %d = %d' % (self.no, count)#time.sleep(self.interval)a+=1def stop(self):self.isstop = Truedef factory():t1 = aa(1,1)t1.start()t2 = aa(2,2)t2.start()time.sleep(20)t1.stop()t2.stop()if __name__=="__main__":factory()
本文探讨了并行开发的基础概念,包括串行与并行的区别、阻塞与非阻塞任务、共享与冲突等问题,并深入介绍了多进程与多线程开发的特点及其实现方法。文中通过具体的Python代码示例展示了fork、wait、waitpid等关键API的使用,以及如何实现进程间的管道通信、创建守护进程、进行多线程编程和使用锁机制。
1724

被折叠的 条评论
为什么被折叠?



