python爬蟲【第2篇】
阿新 • • 發佈:2018-06-22
multi 實現 from eat 進程 lun pid RR print
一、多進程
1.fork方法(os模塊,適用於Lunix系統)
fork方法:調用1次,返回2次。原因:操作系統經當前進程(父進程)復制出一份進程(子進程),兩個進程幾乎完全相同,fork方法分別在父進程、子進程中返回,子進程返回值為0,父進程中返回的是子進程的ID。
普通方法:調用1次,返回1次
import os if __name__ == ‘__main__‘: print ‘current Process (%s) start ....‘%(os.getpid()) #getpid()用戶獲取當前進程ID pid = os.fork() if pid <0:print ‘error in fork‘ elif pid == 0: print ‘I am child process (%s)‘ and my parent process is (%s)‘,(os.getpid(),os.getppid()) else: print ‘I (%s) created a child process (%s).‘,(os.getpid(),pid) 運行結果如下: current Process (3052) start .... I (3052) created a child process (3053). I am child process (3053) and my parent process is (3052)
2.multiprocessing(跨平臺)
import os # 從multiprocessing模塊中導入Process類 from multiprocessing import Process def run_proc(name): print ‘Child process %s (%s) Running...‘ % (name,os.getpid()) if __name__ == ‘__main__‘: print ‘Parent process %s.‘ % os.getpid()for i in range(5): p = Process(target = run_proc,args = (str(i),)) print ‘Process will start‘ #用於啟動進程 p.start() # 用於實現進程間的同步 p.join() print ‘Process end‘ 執行結果如下: Parent process 2392. Process will start. Process will start. Process will start. Process will start. Process will start. Child process 2 (10748) Runing... Child process 0 (5324) Runing... Child process 1 (3196) Runing... Child process 3 (4680) Runing... Child process 4 (10696) Runing... Process end
python爬蟲【第2篇】