推荐学习书目
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 · Dec 21, 2020 · 2445 views
    This topic created in 1978 days ago, the information mentioned may be changed or developed.
    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 replies    2020-12-22 11:33:26 +08:00
    itskingname
        1
    itskingname  
       Dec 21, 2020
    p_jds = multiprocessing.Process(jds.run(), args=())
    第一个参数应该是 jds.run ,不要加括号。
    1462326016
        2
    1462326016  
       Dec 21, 2020   ❤️ 1
    一楼正解,另外在传递函数对象时要时刻注意,不要加括号,加了括号就是执行这个函数,而不是传递函数对象。
    dawnzhu
        3
    dawnzhu  
    OP
       Dec 22, 2020
    @itskingname 是的,没看到这个错误,谢谢哈
    dawnzhu
        4
    dawnzhu  
    OP
       Dec 22, 2020
    @1462326016 对对,没注意到这里,谢谢大佬指点
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1077 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 656ms · UTC 23:19 · PVG 07:19 · LAX 16:19 · JFK 19:19
    ♥ Do have faith in what you're doing.