python爬蟲【第2篇】

來源:互聯網
上載者:User

標籤:end   unix   print   爬蟲   parent   ror   err   tpi   nbsp   

一、多進程

1.fork方法(os模組,適用於Lunix系統)

fork方法:調用1次,返回2次。原因:作業系統經當前進程(父進程)複製出一份進程(子進程),兩個進程幾乎完全相同,fork方法分別在父進程、子進程中返回,子進程傳回值為0,父進程中返回的是子進程的ID。

普通方法:調用1次,返回1次

import osif __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 Processdef 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篇】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.