Python Process blocked by urllib2

后端 未结 1 1250
鱼传尺愫
鱼传尺愫 2021-02-09 02:47

I set up a process that read a queue for incoming urls to download but when urllib2 open a connection the system hangs.

import urllib2, multiprocessing
from thre         


        
1条回答
  •  故里飘歌
    2021-02-09 03:41

    The issue here is not urllib2, but the use of the multiprocessing module. When using the multiprocessing module under Windows, you must not use code that runs immediately when importing your module - instead, put things in the main module inside a if __name__=='__main__' block. See section "Safe importing of main module" here.

    For your code, make this change following in the downloader module:

    #....
    def start():
        global download_worker
        download_worker = Process(target=downloader, args=(url_queue, page_queue))
        download_worker.start()
    

    And in the main module:

    import module
    if __name__=='__main__':
        module.start()
        module.url_queue.put('http://foobar1')
        #....
    

    Because you didn't do this, each time the subprocess was started it would run the main code again and start another process, causing the hang.

    0 讨论(0)
提交回复
热议问题