classification
Title: multiprocessing.Pool.__enter__() should raise an exception if called twice
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: vstinner
Priority: normal Keywords: patch

Created on 2018-12-12 23:08 by vstinner, last changed 2018-12-13 01:15 by vstinner. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 11134 merged vstinner, 2018-12-12 23:13
Messages (3)
msg331719 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-12-12 23:08
On a file, "with file:" fails if it's used a second time:
---
fp = open('/etc/issue')
with fp:
    print("first")
with fp:
    print("second")
---

fails with "ValueError: I/O operation on closed file", because file.__enter__() raises this exception if the file is closed.

I propose to have the same behavior on multiprocessing.Pool.__enter__() to detect when the multiprocessing API is misused.

Anyway, after the first "with pool:" block, the pool becomes unusable to schedule now tasks: apply() raise ValueError("Pool not running") in that case for example.
msg331720 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-12-12 23:16
Currently, the error only occurs when apply() is called:
---
import multiprocessing

def the_test():
    pool = multiprocessing.Pool(1)
    with pool:
        print(pool.apply(int, (2,)))
    with pool:
        print(pool.apply(int, (3,))) # <-- raise here

the_test()
---

I would prefer to get an error on at the second "with pool:" line.
msg331728 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-12-13 01:15
New changeset 08c2ba0717089662132af69bf5948d82277a8a69 by Victor Stinner in branch 'master':
bpo-35477: multiprocessing.Pool.__enter__() fails if called twice (GH-11134)
https://github.com/python/cpython/commit/08c2ba0717089662132af69bf5948d82277a8a69
History
Date User Action Args
2018-12-13 01:15:47vstinnersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-12-13 01:15:34vstinnersetmessages: + msg331728
2018-12-12 23:16:11vstinnersetmessages: + msg331720
2018-12-12 23:13:54vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request10366
2018-12-12 23:08:12vstinnercreate