线程特定的sys.stdout? [英] thread specific sys.stdout?

查看:95
本文介绍了线程特定的sys.stdout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来有点疯狂。我通过

重定向sys.stdout来捕获一个类的输出。然而,另一个线程在

同时运行,偶尔它会向重定向的

sys.stdout输出一些消息,与我想要捕获的输出无关。有没有办法将某些线程特定的重定向输出重新定向?


极光

This may sound a little crazy. I capture the output of one class by
redirecting the sys.stdout. However the is another threading running at
the same time and occasionaly it output some messages to the redirected
sys.stdout irreleveant to the output I want to capture. Is there a way to
redirect output specific to some threads?

aurora

推荐答案

aurora写道:
aurora wrote:
这听起来有点疯狂。我通过重定向sys.stdout来捕获一个类的输出。然而,这是在
同时运行的另一个线程,偶尔它会向重定向的
sys.stdout输出一些消息,与我想要捕获的输出无关。有没有办法将特定于某些线程的输出重定向?
This may sound a little crazy. I capture the output of one class by
redirecting the sys.stdout. However the is another threading running at
the same time and occasionaly it output some messages to the redirected
sys.stdout irreleveant to the output I want to capture. Is there a way to
redirect output specific to some threads?




您可以用分割书面文本的类替换sys.stdout

取决于当前线程。它可能看起来大致如下:


class ThreadPrinter:

def __init __(self):

_.fhs = {}


def写(自我,价值):

f = _.fhs.get(threading.currentThread(),

open (get_some_nice_file_name()," w")

f.write(value)

_.fhs [threading.currentThread()] = f


现在在开始你的线程之前,用一个实例替换sys.stdout

ThreadPrinter:


sys.stdout = ThreadPrinter()

-

问候,


Diez B. Roggisch



You could replace sys.stdout by a class that splits the written text
depending on the current thread. It might look roughly like this:

class ThreadPrinter:
def __init__(self):
_.fhs = {}

def write(self, value):
f = _.fhs.get(threading.currentThread(),
open(get_some_nice_file_name(), "w")
f.write(value)
_.fhs[threading.currentThread()] = f

Now before starting your threads, replace sys.stdout with an instance of
ThreadPrinter:

sys.stdout = ThreadPrinter()
--
Regards,

Diez B. Roggisch


aurora写道:
aurora wrote:
这听起来有点疯狂。我通过重定向sys.stdout捕获一个类的输出。但是另一个线程运行在
同时和偶尔它输出一些消息到重定向的
sys.stdout无关到我想要捕获的输出。是否有一种方法
重定向输出s某些线程特异吗?
This may sound a little crazy. I capture the output of one class by
redirecting the sys.stdout. However the is another threading running at
the same time and occasionaly it output some messages to the redirected
sys.stdout irreleveant to the output I want to capture. Is there a way
to redirect output specific to some threads?




我不知道是否有更简单的方法,但我们曾写过一个

重定向器检查threading.currentThread()以确定

是否应该重定向特定的.write()调用,或者通过sys .__ stdout __将
传递给原始输出。 />

对不起,我再也无法访问代码了,但它不应该很难重现。


-Peter



I don''t know if there''s a simpler way, but we once wrote a
redirector which checked threading.currentThread() to determine
whether a particular .write() call should be redirected or
just passsed through to the original output via sys.__stdout__.

Sorry, I don''t have access to the code any more, but it shouldn''t
be hard for you to reproduce.

-Peter


Diez B. Roggisch写道:
Diez B. Roggisch wrote:
你可以替换sys.stdout根据当前线程分割书面文本的类。它可能看起来大致如下:

类ThreadPrinter:
def __init __(self):
_.fhs = {}

def写(自我) ,值):
f = _.fhs.get(threading.currentThread(),
open(get_some_nice_file_name()," w")
f.write(value)
_.fhs [threading.currentThread()] = f
You could replace sys.stdout by a class that splits the written text
depending on the current thread. It might look roughly like this:

class ThreadPrinter:
def __init__(self):
_.fhs = {}

def write(self, value):
f = _.fhs.get(threading.currentThread(),
open(get_some_nice_file_name(), "w")
f.write(value)
_.fhs[threading.currentThread()] = f




你有没有运行这段代码?它怀疑地看着我/ b
虽然它将在任何给定线程中的第二次写入

调用时引发异常,因为非短路调用

到.get()尝试在写入模式下打开nice_file对于

a第二次。


此外,什么是_应该在这里?自我?


-Peter



Have you run this code? It looks to me suspiciously as
though it will raise an exception on the second write
call in any given thread, as the non-shortcircuiting call
to .get() tries to open the nice_file in write mode for
a second time.

Also, what''s "_" supposed to be here? self?

-Peter


这篇关于线程特定的sys.stdout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆