stdout/stderr for only one thread in a multithreaded python app

2006-07-13 Thread notanotheridiot
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 sys.stdout, but that changes stdout for the whole
application, not just that one thread, which means that any status
updates that I DO want printed to stdout don't get printed.

I've also tried running the code in a seperate process in a new python
interpreter using popen* but I can't read data from stdout until the
program has finished.

Is there some way of passing an overridden value of sys.stdout/stderr
to the exec environment?
(again, I've tried the obvious: exec code in {"sys.stdout": my_stdout}
)

Hoping someone will know something...

Johannes Woolard

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: BaseHTTPRequestHandler: how to read/write until diconnected

2006-07-13 Thread notanotheridiot

Maksim Kasimov wrote:
> I'm trying to write an server-application, using 
> BaseHTTPServer/BaseHTTPRequestHandler.
>
> When application is running, some client-application can post data to the 
> server, and BaseHTTPRequestHandler reads all headers and posted raw data from 
> "rfile", sends back response, than client/server session, than session closed 
> - that works just fine.
>
> how to check whether some data posted again by the client in the session, and 
> send response again in one session (and do such iterations util client stops 
> to post data) ?
>

BaseHTTPServer is precisely that, a HTTP server. what you are
describing is not (I'm pretty sure) not HTTP.
HTTP/1.1 can do something similar and is supported by BaseHTTPServer:
with HTTP/1.1 the stream is not closed between requests. This relies on
a Content-Length field being present in all communications from the
server to the client. To enable HTTP/1.1 you have to set the
protocol_version attribute of your BaseHTTPRequestHandler derived class
to "HTTP/1.1" .

A little while back I wrote a small guide to HTTP in the context of
BaseHTTPServer: http://crunchy.python-hosting.com/wiki/HttpServer

If (as I think) you want to send multiple, inter-dependent messages
each way under the aegis of one HTTP request, you should probably look
at sublassing TCPServer (you can, and probably should, still use
mimetools.Message to parse headers though).

-- 
http://mail.python.org/mailman/listinfo/python-list


Running DocTest on Strings

2006-06-29 Thread notanotheridiot
Hi,
I have two strings - a docstring containing doctests and a code string
containing code to be tested with those doctests. I've been trying for
a day now to run the test without concatenating the two strings,
adding:

import doctest
doctest.testmod

to the bottom, writing it all to a file and executing it using popen().

There must be some way of doing this without writing to a temporary
file, any ideas?

thanks in advance,

johannes Woolard

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running DocTest on Strings

2006-06-30 Thread notanotheridiot

André wrote:
> Paddy wrote:
> > notanotheridiot wrote:
> > > Hi,
> > > I have two strings - a docstring containing doctests and a code string
> > > containing code to be tested with those doctests. I've been trying for
> > > a day now to run the test without concatenating the two strings,
> > > adding:
> > >
> > > import doctest
> > > doctest.testmod
> > >
> > > to the bottom, writing it all to a file and executing it using popen().
> > >
> > > There must be some way of doing this without writing to a temporary
> > > file, any ideas?
> > >
> > > thanks in advance,
> > >
> > > johannes Woolard
> > Create the concatenated string then exec it?
>
> I know from experience that this approach does not work.  When you do
> that, the entire module from which it is run turns out to be scanned
> for doctests - not only the string being executed by exec.  However, I
> understand that Johannes found a solution.
>
> André
>
> >
> > - Pad.

Got it, I've blogged about it here:
http://pytute.blogspot.com/2006/06/say-no-to-temporary-files.html
Basically the trick is to pass the docstring ino the exec environment
as a variable and use the advanced doctest api to get a doctest.

Johannes

-- 
http://mail.python.org/mailman/listinfo/python-list