threadio,适合在多线程环境下捕获stdout。

threadio的Python项目详细描述


threadio,适合在多线程环境下捕获stdout。

>>> # standard capture
>>> from threadio import print_to_file
>>> with print_to_file(TextIO()) as fp:
...     print("hello world")
...
>>> fp.getvalue()
'hello world\n'
>>> # standard capture (but not stdout, stderr)
>>> from threadio import ThreadIO
>>> fp = ThreadIO(TextIO())
>>> with fp.capture(TextIO()) as fp2:
...     print("hello world", file=fp)
...     with fp.capture(TextIO()) as fp3:
...         print("hello world3", file=fp)
...         # not captured fp3, stored fp2
...         print("fail captured", file=fp2)
...
>>> fp.getvalue()
''
>>> fp2.getvalue()
'hello world\nfail captured\n'
>>> fp3.getvalue()
'hello world3\n'
>>> # thread example
>>> from threading import Thread
>>> from threadio import print_to_file
>>> th = Thread(target=print, args=("hello world!",))
>>> with print_to_file(TextIO()) as fp:
...     th.start()
...     th.join()
...     print("HAYO!")
...
hello world!
>>> fp.getvalue() # not capture other thread's print
'HAYO!\n'
>>> # note. print_to_xxx function change everything.
>>> a = sys.stdout
>>> with print_to_file(TextIO()): pass
>>> b = sys.stdout
>>> a is b
False

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
HTTP标头的java InputStream未终止   java测试SpringWebListener   具有定义的替换编号Java的regex ReplaceAll   在java中使用contains()方法   java在选择查询中使用JoinColumn字段   具有用户的java Start-stop demon不是以给定用户启动   java glBufferData生成GL\u无效\u操作   java中循环代码的循环   Java位无符号移位(>>>>)会产生奇怪的结果   java HQL使用点分隔符从select获取结果   条纹、弹簧、玩耍(或?):使用哪种高性能Java框架?   广播接收机中的java停止服务   java回收器视图占据整个屏幕。不在上面显示我的UI元素   java使MySQL查询更快   java MappedByteBuffer查询   java递归算法问题