Issue 35477: multiprocessing.Pool.enter() should raise an exception if called twice (original) (raw)
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.
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.