Python 3: Catching warnings during multiprocessing

后端 未结 3 1140
醉话见心
醉话见心 2021-01-03 19:54

Too long; didn\'t read

The warnings.catch_warnings() context manager is not thread safe. How do I use it in a parallel processing environment?

Background<

3条回答
  •  孤城傲影
    2021-01-03 20:09

    Years later, I finally have a solution (found while working on an unrelated problem). I've tested this on Python 3.7, 3.8, and 3.9.

    Temporarily patch sys.warnoptions with the empty list []. You only need to do this around the call to process.start(). sys.warnoptions is documented as an implementation detail that you shouldn't manually modify; the official recommendations are to use functions in the warnings module and to set PYTHONWARNINGS in os.environ. This doesn't work. The only thing that seems to work is patching sys.warnoptions. In a test, you can do the following:

    import multiprocessing
    from unittest.mock import patch
    p = multiprocessing.Process(target=my_function)
    with patch('sys.warnoptions', []):
        p.start()
    p.join()
    

    If you don't want to use unittest.mock, just patch by hand:

    import multiprocessing
    import sys
    p = multiprocessing.Process(target=my_function)
    old_warnoptions = sys.warnoptions
    try:
        sys.warnoptions = []
        p.start()
    finally:
        sys.warnoptions = old_warnoptions
    p.join()
    

提交回复
热议问题