473,382 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

thread specific 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
Jul 18 '05 #1
15 12899
aurora wrote:
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?


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
Jul 18 '05 #2
aurora wrote:
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?


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
Jul 18 '05 #3
Diez B. Roggisch wrote:
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


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
Jul 18 '05 #4
Peter Hansen wrote:
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.
Nope, didn't run it - and you are right of course. I should have said more
clearly that the code was untested - I thought that describing it as

"might look roughly like this"

would suffice.
Also, what's "_" supposed to be here?**self?


Yup it is - I use _ for self - I tried to adapt to the common standard for
the post, but failed in the middle of it. Sorry for the confusion.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #5
Diez B. Roggisch wrote:
Peter Hansen wrote:
Have you run this code?


Nope, didn't run it - and you are right of course. I should have said more
clearly that the code was untested - I thought that describing it as

"might look roughly like this"

would suffice.


That probably would have sufficed, but I'm one of those people
that tends not to read the documentation. I just jumped to
the code. ;-)

-Peter
Jul 18 '05 #6
On Wed, 15 Sep 2004 23:14:47 +0200, Diez B. Roggisch <de*********@web.de>
wrote:
aurora wrote:
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?


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()


Thanks this is a nice idea. I hope Python would actually support the '_'
syntax. The self really reduce readablity, especially if you have several
of them in one line.

Jul 18 '05 #7
aurora wrote:
On Wed, 15 Sep 2004 23:14:47 +0200, Diez B. Roggisch
class ThreadPrinter:
def __init__(self):
_.fhs = {}

def write(self, value):
f = _.fhs.get(threading.currentThread(),
.....
Thanks this is a nice idea. I hope Python would actually support the
'_' syntax. The self really reduce readablity, especially if you have
several of them in one line.


It does! One just has to be consistent within each function.
Diez changed the code from something like this:

def __init__(_):
_.fhs = {}

def write(_, value):
f = _.fhs.get(threading.currentThread(),
....

Some would argue that this is actually less readable, however,
since it uses punctuation instead of a word. If nothing else,
you run into a bit of a conflict between your own technique,
with "_", and the vast majority of the rest of the Python world,
which uses "self" exclusively, leading to situations like this
one...

(I think if I had a routine that really heavily used self,
to the obvious detriment of readability, and it wasn't clear
how else to improve it, I would use a local assignment at
the top to make a shorter name, perhaps "s", or even "_" --
but I wouldn't use the possibility of such a thing as a
justification for using _ everywhere.)

-Peter
Jul 18 '05 #8
Peter Hansen wrote:
aurora wrote:
On Wed, 15 Sep 2004 23:14:47 +0200, Diez B. Roggisch
class ThreadPrinter:
def __init__(self):
_.fhs = {}

def write(self, value):
f = _.fhs.get(threading.currentThread(),


....
Thanks this is a nice idea. I hope Python would actually support the
'_' syntax. The self really reduce readablity, especially if you have
several of them in one line.

It does! One just has to be consistent within each function.
Diez changed the code from something like this:

def __init__(_):
_.fhs = {}

def write(_, value):
f = _.fhs.get(threading.currentThread(),
...

Some would argue that this is actually less readable, however,
since it uses punctuation instead of a word. If nothing else,
you run into a bit of a conflict between your own technique,
with "_", and the vast majority of the rest of the Python world,
which uses "self" exclusively, leading to situations like this
one...

(I think if I had a routine that really heavily used self,
to the obvious detriment of readability, and it wasn't clear
how else to improve it, I would use a local assignment at
the top to make a shorter name, perhaps "s", or even "_" --
but I wouldn't use the possibility of such a thing as a
justification for using _ everywhere.)

-Peter


Didn't aware that _ itself is a valid identifier! True, you don't really
want to do things differently from convention. I'm just ranting about
the verbosity of self.

This doesn't take a complicated statement to make it really clumsy. Some
simple statement would look like this:

if self.max < self.list[self.index]:
self.max = self.list[self.index]:

Replacing self with _, depends on one's aesthetic, it could be ugly or
it could be cleaner. I like it that it is not a word and it does not
interfere with the keywords that's really relevant.

if _.max < _.list[_.index]:
_.max = _.list[_.index]:

Of couse I think this syntax the best:

if max < list[index]:
max = list[index]:

This remind me of those awful Hungarian notation.

aurora
Jul 18 '05 #9
aurora <au******@gmail.com> wrote:
...
Of couse I think this syntax the best:

if max < list[index]:
max = list[index]:
Just to ensure that the best approach,
self.max = max(self.max, self.list[index])
isn't available any more, _and_ you can't use the list built-in name any
more either? What a scoop!

This remind me of those awful Hungarian notation.


Explicit scope denotation, and Hungarian notation (which prefixes names
with type-connected prefix strings), have essentially nothing to do with
each other, of course. Making classes implicit scopes (like, say, C++,
but differently from, say, Modula-3) is simply a horrid mess, where you
can't use bare names safely without carefully studying all the internals
of all your ancestor classes... and if any such ancestor ever adds a
private name it can break every subclass which _did_ use bare names. A
bad idea even in a language where the compiler can find out statically
where every name comes from (because human readers can't), just as bad
as "from foo import *" in Python or "using namespace foo" in C++ except
that you can't avoid it by just eschewing one misdesigned construct.

In a language where even the compiler _cannot_ tell statically which
bare names come from where (except for functions' locals), criticizing
the language design choice of _not_ making classes into implcit scopes
doesn't even _verge_ on the ridiculous -- it plunges right deep into it.
Alex
Jul 18 '05 #10
Diez B. Roggisch <de*********@web.de> wrote:
aurora wrote:
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?


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


Not a bad general idea, but you need a better implementation of the
"thread-local storage" design pattern than just a bare dictionary like
this 'fhs' dict. In Python 2.4, threading.local gives you such an
implementation. If you need to work in Python 2.3, it's more work, but
there are cookbook recipes (on Activestate's site) which can help.
Alex
Jul 18 '05 #11
Alex Martelli wrote:
aurora <au******@gmail.com> wrote:
...
Of couse I think this syntax the best:

if max < list[index]:
max = list[index]:

Just to ensure that the best approach,
self.max = max(self.max, self.list[index])
isn't available any more, _and_ you can't use the list built-in name any
more either? What a scoop!
This remind me of those awful Hungarian notation.

Explicit scope denotation, and Hungarian notation (which prefixes names
with type-connected prefix strings), have essentially nothing to do with
each other, of course. Making classes implicit scopes (like, say, C++,
but differently from, say, Modula-3) is simply a horrid mess, where you
can't use bare names safely without carefully studying all the internals
of all your ancestor classes... and if any such ancestor ever adds a
private name it can break every subclass which _did_ use bare names. A
bad idea even in a language where the compiler can find out statically
where every name comes from (because human readers can't), just as bad
as "from foo import *" in Python or "using namespace foo" in C++ except
that you can't avoid it by just eschewing one misdesigned construct.

In a language where even the compiler _cannot_ tell statically which
bare names come from where (except for functions' locals), criticizing
the language design choice of _not_ making classes into implcit scopes
doesn't even _verge_ on the ridiculous -- it plunges right deep into it.
Alex


I'm not making any serious criticism on the language or how it should do
name binding. I'm ranting about having to attach a prefix to names often
make simple things look complicated. An annoyance when it has to be done
very often. You got the point?
Jul 18 '05 #12
aurora <au******@gmail.com> wrote in message news:<10*************@corp.supernews.com>...
Peter Hansen wrote:
aurora wrote:
On Wed, 15 Sep 2004 23:14:47 +0200, Diez B. Roggisch

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

def write(self, value):
f = _.fhs.get(threading.currentThread(),


....
Thanks this is a nice idea. I hope Python would actually support the
'_' syntax. The self really reduce readablity, especially if you have
several of them in one line.

It does! One just has to be consistent within each function.
Diez changed the code from something like this:

def __init__(_):
_.fhs = {}

def write(_, value):
f = _.fhs.get(threading.currentThread(),
...

Some would argue that this is actually less readable, however,
since it uses punctuation instead of a word. If nothing else,
you run into a bit of a conflict between your own technique,
with "_", and the vast majority of the rest of the Python world,
which uses "self" exclusively, leading to situations like this
one...

(I think if I had a routine that really heavily used self,
to the obvious detriment of readability, and it wasn't clear
how else to improve it, I would use a local assignment at
the top to make a shorter name, perhaps "s", or even "_" --
but I wouldn't use the possibility of such a thing as a
justification for using _ everywhere.)

-Peter


Didn't aware that _ itself is a valid identifier! True, you don't really
want to do things differently from convention. I'm just ranting about
the verbosity of self.

This doesn't take a complicated statement to make it really clumsy. Some
simple statement would look like this:

if self.max < self.list[self.index]:
self.max = self.list[self.index]:

Replacing self with _, depends on one's aesthetic, it could be ugly or
it could be cleaner. I like it that it is not a word and it does not
interfere with the keywords that's really relevant.

if _.max < _.list[_.index]:
_.max = _.list[_.index]:

Of couse I think this syntax the best:

if max < list[index]:
max = list[index]:

This remind me of those awful Hungarian notation.

aurora

Please do not do this, please...
Recently I was working with a modules written by a programmer,
who used "_" instead of "self".
This was so "not in line" with the rest of the system,
that it took me extra hour or two to get accustomed
and switch back and force (self in "standard" modules and _ in "economical").
Jul 18 '05 #13
>>>>> "aurora" == aurora <au******@gmail.com> writes:
aurora> I'm not making any serious criticism on the language or
aurora> how it should do name binding. I'm ranting about having to
aurora> attach a prefix to names often make simple things look
aurora> complicated. An annoyance when it has to be done very
aurora> often. You got the point?

Why don't you write a preprocessor that converts every .foo to
self.foo?

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #14
aurora <au******@gmail.com> wrote:
...
I'm not making any serious criticism on the language or how it should do
name binding. I'm ranting about having to attach a prefix to names often
make simple things look complicated. An annoyance when it has to be done
very often. You got the point?


No. If you had to "attach a prefix", it might perhaps be a problem.
But, in Python, you don't have to do any such thing. You use bare names
for locals, globals, and built-ins, and compound names for attributes of
objects. How can it "make simple things look complicated" to use a
compound name for an object attribute? It indicates exactly what's
going on: you're accessing or rebinding an attribute -- no more, no
less. Just like, e.g., x[i] is how you refer to an item of x, so is
x.foo how you refer to an attribute of x.

If what you mean to say (as opposed to what you actually wrote) is to
support some kind of 'with' or 'using' statement, such as:

with glek[idx]:
.total += .partial
.partial = current_time
.laps += 1

so that several accesses to, and rebindings of, attributes of the same
object, can be compactly and conveniently indicated, that, of course, is
quite a different issue, which has often been discussed in the past,
both here and on python-dev. Yet there isn't even a PEP for it, yet, I
believe -- a bad sign: it suggests nobody is keen enough on it to
summarize the case for and against and nail down the specs. I guess the
problem is that if you see this as equivalent to, say:

_ = glek[idx]:
_.total += _.partial
_.partial = current_time
_.laps += 1

it's not all that clear that the with notation is buying you all that
much -- visually distinguishing that current_time is not an attribute
name is about as hard in both cases, for example, maybe a bit harder if
the hypothetical new construct were in use... and without said
hypothetical new construct you have a better chance to make your code
clearer by using a visually distinguished name rather than a notation
which appears to try to HIDE the crucial issue of which name are bare
ones, and which names are compound!

Richer semantics for 'with'/'using' are of course possible, but harder
to pin down in detail, and even more controversial. Still, until
somebody DOES care enough, for or against, to come up with a PEP, rather
than rants on this NG, nothing much is going to happen re this idea.
Alex
Jul 18 '05 #15
On Fri, 17 Sep 2004 08:54:11 +0200, al*****@yahoo.com (Alex Martelli) wrote:
aurora <au******@gmail.com> wrote:
...
I'm not making any serious criticism on the language or how it should do
name binding. I'm ranting about having to attach a prefix to names often
make simple things look complicated. An annoyance when it has to be done
very often. You got the point?


No. If you had to "attach a prefix", it might perhaps be a problem.
But, in Python, you don't have to do any such thing. You use bare names
for locals, globals, and built-ins, and compound names for attributes of
objects. How can it "make simple things look complicated" to use a
compound name for an object attribute? It indicates exactly what's
going on: you're accessing or rebinding an attribute -- no more, no
less. Just like, e.g., x[i] is how you refer to an item of x, so is
x.foo how you refer to an attribute of x.

If what you mean to say (as opposed to what you actually wrote) is to
support some kind of 'with' or 'using' statement, such as:

with glek[idx]:
.total += .partial
.partial = current_time
.laps += 1

so that several accesses to, and rebindings of, attributes of the same
object, can be compactly and conveniently indicated, that, of course, is
quite a different issue, which has often been discussed in the past,
both here and on python-dev. Yet there isn't even a PEP for it, yet, I
believe -- a bad sign: it suggests nobody is keen enough on it to
summarize the case for and against and nail down the specs. I guess the
problem is that if you see this as equivalent to, say:

_ = glek[idx]:
_.total += _.partial
_.partial = current_time
_.laps += 1

it's not all that clear that the with notation is buying you all that
much -- visually distinguishing that current_time is not an attribute
name is about as hard in both cases, for example, maybe a bit harder if
the hypothetical new construct were in use... and without said
hypothetical new construct you have a better chance to make your code
clearer by using a visually distinguished name rather than a notation
which appears to try to HIDE the crucial issue of which name are bare
ones, and which names are compound!

Richer semantics for 'with'/'using' are of course possible, but harder
to pin down in detail, and even more controversial. Still, until
somebody DOES care enough, for or against, to come up with a PEP, rather
than rants on this NG, nothing much is going to happen re this idea.

It occurs to me that "with obj_expr:" selects a specific name space and
access mechanism (attribute access, which looks through base classes for
decriptors etc), whereas one could expand the concept of what the leading
dot means as an access operation. E.g., the normal default spelled out might be

with(glek[idx], dotop=with.attr): # with.attr would be a get/set pair for attributes
.total += .partial
...

Or you could limit yourself to an instance dict by alternate meaning
for '.' access, e.g.,

with(vars(inst), dotop=with.item): # with.item would be get/set for items
.total += .partial # meaning v=vars(inst); v['total'] += v['partial']; del v
...

or you could specify a list of spaces to chase in explicit order, e.g.,
object attribute spaces:

with(a,b,c):
x = .xxx # a.xxx if avail, else b.xxx, else c.xxx, else AttributeError

Anyway, you get the general idea. A way to spell out access to any alternative
name space or search path through several. ;-)

Regards,
Bengt Richter
Jul 18 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.1 (final). Python 2.3.1 is a pure bug fix release of Python 2.3, released in...
2
by: Jon Wright | last post by:
Trying to work around the lack of pexpect for native windows python I had a play with os.popen2 and some threads. This looks promising enough for what I want to do, but I hit on a problem with...
13
by: Deepak Sarda | last post by:
Hello everyone. I have run into something which I believe is a bug or a shortcoming of the threading.Thread module. My program spawns 15 threads. For this I've creating a new class with...
0
by: Michael Hobbs | last post by:
I just wanted to see if anyone else experienced a problem with the Thread.join() method in Python 2.4. Unfortunately, I did not debug this problem fully before re-writing my code to avoid...
1
by: Toon Verstraelen | last post by:
Hi, I recently had a thread problem and I could reduce it to a very short example that shows the problem. I hope it has its origin in my misunderstanding of how python threads work. Here it is:...
16
by: diffuser78 | last post by:
I want to write a python program and call OS specific commands in it. So basically, instead of typing in on the command line argument I want to have it in a python program and let it do the action....
1
by: notanotheridiot | last post by:
Hi- I'm trying to exec some arbitrary code in one thread of an application and read anything it prints to stdout or stderr in another thread. My question is how? I've tried changing...
1
by: [david] | last post by:
What am I doing wrong? I'm trying to capture stdErr in a multi-threaded program. This code crashes wxPython with /Py Assertion Error: C++ assertion "m_count=-1 || m_count=-2" failed/ What I'm...
0
by: Gabriel Genellina | last post by:
En Wed, 16 Apr 2008 16:29:48 -0300, Marlin Rowley <marlin_rowley@hotmail.comescribió: Replace sys.stdout with an object that stores the lines printed. (Due to the way the print statement...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.