Winter Madness - Passing Python objects as Strings

Hendrik van Rooyen mail at
Fri Jun 5 04:54:47 EDT 2009

<skip at> wrote:

> Got some use cases?

plural cases - no.

I did it for the reason already described.
to elucidate, the code looks something like this:

rec = input_q.get()  # <=== this has its origen in a socket, as a netstring.
reclist = rec.split(',')
if reclist[0] == 'A':
    do something with the outputs
    get hold of the latest inputs
    return the result by putting a message on the normal output q.
# up to here this is the code that is done in 99.9999% of cases.
# note that it has to run as fast as possible, in a very cripple processor.
if reclist[0] == "B":   # This means we have to change state, 
                                # it comes from another thread that did
                                # not exist before an event.
   new_output_q = uncan(reclist[1]) # <==  This is where it is used
   while True:
        do similar stuff until normality is re established,
        discarding the incoming "A" records,
        using new "C" records and new_output_q.
        Terminated by a "D" record.
It is simply a different way of saying "use this one", 
in an in band way.

In the above, it avoids a double unpacking step - once to
get to the record type, and then to get to the actual data.
It only makes sense here because I know that the stuff
that comes in is basically an unending stream of more
of the same, and it happens - I would say thousands of 
times a second, but it is more like a hundred or so, given 
the lack of speed of the processor.

So I am quite prepared to trade off the slight inefficiency
during the seldom occurring changeover for the railroad
like chugging along in the vast majority of cases.

"seldom" here is like once a day for a few minutes.

And it sure beats the hell out of passing the queue 
name as a string and mucking around with exec or eval -
That is what I did first, and I liked it even less,
as the queue passed in such a way had to be a global
for the exec to work.

It all started because I was not prepared to waste
precious cycles in an extra unpacking stage.

So I wrote the Can extension module, and
I thought it weird enough to make it public: - 
Hands up those who have ever passed a 
pointer as a string !

- Hendrik

More information about the Python-list mailing list