Подавить стандартный вывод сопрограммы, не затрагивая другие сопрограммы в PythonPython

Программы на Python
Anonymous
Подавить стандартный вывод сопрограммы, не затрагивая другие сопрограммы в Python

Сообщение Anonymous »

Код: Выделить всё

import asyncio
import contextlib
import os

async def my_coroutine():
with open(os.devnull, 'w') as f, contextlib.redirect_stdout(f):
print("This will not be printed")
await asyncio.sleep(1)
print("This will be printed")

async def my_coroutine2():
print("This will be printed from 2")
await asyncio.sleep(1)

async def main():
await asyncio.gather(
my_coroutine(),
my_coroutine2()
)

asyncio.run(main())
В приведенном выше случае результат будет

Код: Выделить всё

This will be printed
Есть ли способ распечатать

Код: Выделить всё

This will be printed from 2
This will be printed
Это означает, что я хочу подавить стандартный вывод только my_coroutine, не затрагивая my_coroutine2


Подробнее здесь: https://stackoverflow.com/questions/792 ... -in-python

Вернуться в «Python»