weixin_46641005 2020-05-13 16:47 采纳率: 100%
浏览 1074
已采纳

多进程在运行的时候只有一个子进程会运行,怎么解决?

我用python写的多进程爬虫,在linux服务器上运行的时候,不管我设置多少个进程,每次都只有一个子进程运行。
主函数是这样的,mainFunction是我爬虫的主要函数,现在每次都只有第一个进程运行,就是参数是0的那一个。
因为爬虫I/O不算密集,所以没用多线程。另外进程之间是独立的。
我的目标是10个进程同时运行,我有10个API的key,数据也分成10块来抓,这里mainFunction里的参数就是为了对应要用的key和要抓的数据。

if __name__ == '__main__':
    print('Started at ', datetime.now())

    processes = []
    for i in range(0,10):
        t = multiprocessing.Process(target = mainFunction, args = (i,))
        processes.append(t)
    for t in processes:
        t.start()
        t.join()

    print('Finished at ', datetime.now())


麻烦大佬们帮忙找找原因,谢谢!

  • 写回答

1条回答 默认 最新

  • bobhuang 2020-05-13 18:14
    关注

    这段代码,每次循环都是start一个线程,并等待结束后再开始下一个线程。

        for t in processes:
            t.start()
            t.join()
    

    改为:

        for t in processes:
            t.start()
        for t in processes:
            t.join()
    

    完整的测试代码:

    import time
    import multiprocessing
    from datetime import datetime
    
    def mainFunction(idx,running):
        print('thread {0} is running.'.format(idx))
        while(running.value):
            pass
        print('thread {0} is closing .'.format(idx))
    
    if __name__ == '__main__':
        running = multiprocessing.Value('i',1)
        print('Started at ', datetime.now())
        processes = []
        for i in range(0,10):
            t = multiprocessing.Process(target = mainFunction, args = (i,running,))
            processes.append(t)
        for t in processes:
            t.start()
    
        time.sleep(10)
        running.value = 0
    

    运行结果:

    ('Started at ', datetime.datetime(2020, 5, 14, 11, 9, 47, 862220))
    thread 0 is running.
    thread 1 is running.
    thread 2 is running.
    thread 3 is running.
    thread 4 is running.
    thread 5 is running.
    thread 7 is running.
    thread 6 is running.
    thread 9 is running.
    thread 8 is running.
    thread 1 is closing.
    thread 7 is closing.
    thread 2 is closing.
    thread 3 is closing.
    thread 0 is closing.
    thread 5 is closing.
    thread 4 is closing.
    thread 9 is closing.
    thread 6 is closing.
    thread 8 is closing.
    ('Finished at ', datetime.datetime(2020, 5, 14, 11, 9, 57, 989041))
    

    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何实现uniapp编译的微信小程序做可回溯视频
  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 AttributeError: 'EasyDict' object has no attribute 'BACKUP_DB_INFO'
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转
  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息