首页 > 代码库 > python 并发编程入门
python 并发编程入门
多进程
在Unix/Linux下,为我们提供了类似c中<unistd.h>头文件中的的fork()函数的接口,这个函数位于os模块中,同样与c中类似,对于父进程fork()调用返回子进程ID,对于子进程返回0
import os, timepid = os.fork()if pid == 0: while True: print 'child process' time.sleep(1)else: while True: print 'parent process' time.sleep(3)
考虑到Windows并没有这个调用,python为我们提供了跨平台的版本,这就是multiprocessing模块,通过multiprocessing模块中的Process类可实现跨平台的多进程,用法非常简单
#coding:utf-8from multiprocessing import Processimport os, timedef handler(args): print 'process parameter is %s' % args while True: print 'child process' time.sleep(1)if __name__=='__main__': print 'parent process is %d' % os.getpid() child_proc = Process(target = handler, args=('test parameter',)) #指定子进程开始执行的函数 child_proc.start() while True: print 'parent process' time.sleep(3)
注意:若不加if __name__==‘__main__‘,子进程启动后会将模块内的代码再执行一遍,为避免不必要的错误,应该加上它
Python为了更方便的使用多进程还提供了进程池Pool, 位于multiprocessing模块中,进程池用于对于并发响应要求较高的条件中,预先分配进程,节省了处理过程中fork的开销
关于更多进程池的内容可参考 http://blog.csdn.net/aspnet_lyc/article/details/38946915#t3 中的TCP预先派生子进程服务器
#coding:utf-8from multiprocessing import Poolimport os, time, randomdef handler(proc_args): print proc_argsif __name__ == '__main__': pool = Pool(4) #设置进程池中的进程数 for loop in range(4): pool.apply_async(handler, args=(loop,)) #apply_async(func,args),从进程池中取出一个进程执行func,args为func的参数。返回一个 AsyncResult的对象,对该对象调用get()方法可以获得结果。 pool.close() #不在往进程池中添加进程 pool.join() #等待所有子进程结束 print 'All child processes done'
多线程
Python中对于多线程提供了thread和threading模块, threading对thread进行了封装,更易用,python官网的描述如下
This module provides low-level primitives for working with multiple threads (also called light-weight processes or tasks) — multiple threads of control sharing their global data space. For synchronization, simple locks (also called mutexes or binary semaphores) are provided. The threading module provides an easier to use and higher-level threading API built on top of this module
调用thread模块中的start_new_thread()函数来产生新线程
import threaddef thread_handler(args): print argsif __name__ == '__main__': thread.start_new_thread(thread_handler, ('test parameter',)) while True: pass
将线程函数传入并创建Thread实例,然后调用start()创建线程并执行
import threadingdef thread_handler(args): print argsif __name__ == '__main__': th1 = threading.Thread(target=thread_handler, args=('test parameter 1',)) th2 = threading.Thread(target=thread_handler, args=('test parameter 2',)) th1.start() th2.start() th1.join() th2.join() print 'All threads ended'
python 并发编程入门