V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
dawnzhu
V2EX  ›  Python

Python 创建多进程,运行程序确没有并行 执行,没搞明白,有大佬指点一二吗?

  •  
  •   dawnzhu · 2020-12-21 17:44:22 +08:00 · 1582 次点击
    这是一个创建于 1222 天前的主题,其中的信息可能已经有所发展或是发生改变。
    if __name__ == "__main__":
        initial_time = time.time()  # 程序开始时间
        url_pool = initialize_database()  # 初始化数据库
        search_key = [
                    "BALENCIAGA 包",
                    "BALLY 包",
                    "BOTTEGA VENETA 包",
                    "BURBERRY 包",
                    "BVLGARI 包",
                    "CELINE 包",
                    "CHANEL 包"
                    ]
        jds = Jd_Spider(url_pool, 'jd_info')  # 实例化类
        p_list = []
        p_jds = multiprocessing.Process(jds.run(), args=())
        p_list.append(p_jds)
        for key in search_key:
            p = multiprocessing.Process(search_keyword.search_sku, args=(key,))
            p_list.append(p)
        for P_step in p_list:
            P_step.start()
        for p_step in p_list:
            p_step.join()
        print("最终用时:", time.time() - initial_time)  # 打印程序最终用时
    

    我创建多个进程,运行程序,总是会执行 jds_run() 这个函数,search_key_word.search_sku 没有得到并发执行,有大佬清楚其中缘由吗

    4 条回复    2020-12-22 11:33:26 +08:00
    itskingname
        1
    itskingname  
       2020-12-21 18:11:46 +08:00
    p_jds = multiprocessing.Process(jds.run(), args=())
    第一个参数应该是 jds.run ,不要加括号。
    1462326016
        2
    1462326016  
       2020-12-21 18:37:36 +08:00   ❤️ 1
    一楼正解,另外在传递函数对象时要时刻注意,不要加括号,加了括号就是执行这个函数,而不是传递函数对象。
    dawnzhu
        3
    dawnzhu  
    OP
       2020-12-22 11:32:56 +08:00
    @itskingname 是的,没看到这个错误,谢谢哈
    dawnzhu
        4
    dawnzhu  
    OP
       2020-12-22 11:33:26 +08:00
    @1462326016 对对,没注意到这里,谢谢大佬指点
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1847 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 16:42 · PVG 00:42 · LAX 09:42 · JFK 12:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.