cpython icon indicating copy to clipboard operation
cpython copied to clipboard

all deprecation warnings are disabled for the whole process while new unix asyncio subprocesses are created

Open graingert opened this issue 3 years ago • 5 comments

on python3.10 the following prints:

python3.10 demo.py 
/home/graingert/projects/cpython/demo.py:40: DeprecationWarning: demo warning
  warnings.warn("demo warning", DeprecationWarning)
/home/graingert/projects/cpython/demo.py:48: DeprecationWarning: demo warning2
  warnings.warn("demo warning2", DeprecationWarning)

but on python main it prints:

./python demo.py      
/home/graingert/projects/cpython/demo.py:40: DeprecationWarning: demo warning
  warnings.warn("demo warning", DeprecationWarning)
import sys
import threading
import asyncio
import concurrent.futures
import warnings


async def acreate_process_then_sleep(started_event, stop_event):
    event = asyncio.Event()

    async def create_process():
        loop = asyncio.get_running_loop()
        loop.call_soon(event.set)
        proc = await asyncio.create_subprocess_exec(
            sys.executable,
            "-c",
            "print('hello')",
            stdin=None,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.STDOUT,
        )
        await proc.communicate()

    async def sleep():
        await event.wait()
        started_event.set()
        stop_event.wait()

    await asyncio.gather(create_process(), sleep())


def create_process_then_sleep(*args, **kwargs):
    asyncio.run(acreate_process_then_sleep(*args, **kwargs))


def main():
    with concurrent.futures.ThreadPoolExecutor() as tpe:
        stop_event = threading.Event()
        started_event = threading.Event()
        warnings.warn("demo warning", DeprecationWarning)
        fut = tpe.submit(
            create_process_then_sleep,
            started_event=started_event,
            stop_event=stop_event,
        )
        try:
            started_event.wait()
            warnings.warn("demo warning2", DeprecationWarning)
        finally:
            stop_event.set()
            fut.result()


if __name__ == "__main__":
    sys.exit(main())

Originally posted by @graingert in https://github.com/python/cpython/pull/98215#discussion_r996426251

graingert avatar Oct 16 '22 11:10 graingert

cc @kumaraditya303

graingert avatar Oct 16 '22 11:10 graingert

see also https://github.com/python/cpython/issues/91505, https://github.com/python/cpython/issues/81785 and https://github.com/python/cpython/issues/50896

graingert avatar Oct 16 '22 11:10 graingert

Okay, I'll be AFK for some time so feel free to create a PR. I doubt that any one will hit this in practise though.

kumaraditya303 avatar Oct 16 '22 12:10 kumaraditya303

Ah, I guess I was right about that context manager. :-)

gvanrossum avatar Oct 16 '22 15:10 gvanrossum

https://github.com/python/cpython/pull/98333 fixes this, I had underestimated warnings module :-)

kumaraditya303 avatar Oct 16 '22 15:10 kumaraditya303

We're still looking for a perfect solution (see discussion in PR), so keeping this issue open.

gvanrossum avatar Oct 17 '22 15:10 gvanrossum

I'm closing this as done with good enough, there are plenty of places in stdlib where this happens and in those places some real work in done rather than just a function call. No point in spending more time on this.

kumaraditya303 avatar May 11 '23 10:05 kumaraditya303