V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
oldbird
V2EX  ›  Python

多进程处理数据时如何完整打印?

  •  
  •   oldbird · Jan 12, 2021 · 2830 views
    This topic created in 1936 days ago, the information mentioned may be changed or developed.

    from multiprocessing import Pool

    def do(item):

    	print item
    
    	...... #处理 txt (每个大概 5 分钟)
    

    if name=='main':

    	items=glob.glob('*.txt')
    
    	pool=Pool(5)
    
    	pool.map(do,items)
    
    	pool.close()
    
    	pool.join()
    

    items 为['1111.txt','2222.txt','3333.txt','4444.txt','5555.txt',......'9999.txt']

    运行时会穿插打印,比如 111221.33txt

    是否有办法将每项打印完整?

    8 replies    2021-02-28 13:49:46 +08:00
    maocat
        1
    maocat  
       Jan 12, 2021
    上锁,改同步
    crackhopper
        2
    crackhopper  
       Jan 12, 2021
    还可以用 queue,有个线程安全的 queue,用 multiprocessing.Manager,上面带了一个 queue 。
    BrokenVns
        3
    BrokenVns  
       Jan 12, 2021
    再启动一个日志进程,通过 socket 把每个进程的日志发送过去,由日志进程汇总打印。
    UN2758
        4
    UN2758  
       Jan 12, 2021
    可以用带队列功能的日志,比如 loguru
    jones2000
        6
    jones2000  
       Jan 12, 2021
    还不如每个进程一个日志文件, 每行日志加时间戳,最后显示的时候读多个文件日志,根据时间戳排序下。 省事
    oldbird
        7
    oldbird  
    OP
       Feb 28, 2021
    @jones2000 请问每个进程一个日志该怎么写? 目前只会对每个进程中的每个处理文件一个日志……
    jones2000
        8
    jones2000  
       Feb 28, 2021
    @oldbird 我直接用 log4net 插件, 它有统一的写日志的接口.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2414 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 03:52 · PVG 11:52 · LAX 20:52 · JFK 23:52
    ♥ Do have faith in what you're doing.