[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

python interactive - threaded console (run -i

Aaron Brady

3/11/2008 4:19:00 AM

#interactive run -i
import threading
import Queue
import functools
class GenThread( threading.Thread, Queue.Queue ):
_taskid= 0
def __init__( self, *ar, **kw ):
threading.Thread.__init__( self, *ar, **kw )
Queue.Queue.__init__( self )
self.setDaemon( True )
self.start()
self._doneevents= {}
def run( self ):
while 1:
id, fun, ar, kw= self.get()
self._= fun( *ar, **kw )
self._doneevents[id].set()
if self._:
self._print( self._ )
def me( self, fun, *ar, **kw ):
id= GenThread._taskid
GenThread._taskid+= 1
self.put( ( id, fun, ar, kw ) )
self._doneevents[ id ]= e= threading.Event()
return e
def _print( self, *ar, **kw ):
print( self.getName(), *ar, **kw )

for i in range( 10 ):
exec( 'th%i= GenThread()'% i )

import socket
host= socket.socket( socket.AF_INET,
socket.SOCK_STREAM )
host.bind( ( '', 8000 ) )
th1.me( host.listen, 1 )
_acc= th1.me( host.accept )
cli= socket.socket( socket.AF_INET,
socket.SOCK_STREAM )
th2.me( cli.connect, ( 'localhost', 8000 ) )
_acc.wait()
conn= th1._[0]

ConnClose= object()
class IncompleteTransmission( Exception ): pass
def _draintilclose( conn, understandfun= None ):
while 1:
_preamb= ''
while 1:
_data= conn.recv( 1 )
if not _data:
conn.close()
return ConnClose
if _data== b'\x00':
break
_preamb+= _data.decode()
_len= int( _preamb, 16 )
_lenleft= _len
_curmsg= bytearray()
while _lenleft:
_data= conn.recv( min( 4096, _lenleft ) )
if not _data:
raise IncompleteTransmission
_curmsg.extend( _data )
_lenleft-= len( _data )
assert len( _curmsg )== _len
if None is not understandfun:
understandfun( _curmsg )

def _dressandsend( sck, msg ):
_preamble= hex( len( msg ) )[2:]+ '\x00'
_dressed= bytes( _preamble, 'ascii' )+ msg
_lenleft= len( _dressed )
while _lenleft:
_sent= sck.send( _dressed[-_lenleft:] )
_lenleft-= _sent

th1.me( _draintilclose, conn, th1._print )
th2.me( _draintilclose, cli, th2._print )

This is cool! Set up a socket and listen 'til close on one thread,
print output by default as complete messages are received. Listen
'til close, same, on another, th1, th2 respectively. Roughly off the
console:

>>> _dressandsend( conn, b'abc' )
Thread-2 bytearray(b'abc')
>>> _dressandsend( cli, b'def' )
Thread-1 bytearray(b'def')

Rearranged the '>>>'. It's damn annoying. Probably a specialized
stdout to detect when in a prompt and add extra newline, maybe
modified GenThread._print. Someone go replace hex with base255 and
conn.recv( 1 ) with conn.recv( 2 ). Should take any length-- base256
over hex is just a factor of 2, and still O( log( message length ) ).

>>> dir()
['ConnClose', 'GenThread', 'IncompleteTransmission', 'Queue',
'__builtins__', '__doc__', '__name__', '__package__', '_acc',
'_draintilclose', '_dressandsend', 'cli', 'conn', 'functools', 'host',
'i', 'socket', 'th1', 'th2', 'th3', 'th4', 'th5', 'th6', 'th7', 'th8',
'th9', 'threading']
10 Answers

Aaron Brady

3/11/2008 9:54:00 PM

0

>         self._doneevents= {}

is extraneous.

> id, fun, ar, kw= self.get()
done, fun, ar, kw= self.get()

suffices.

> def _draintilclose( conn, understandfun= None ):
>         if None is not understandfun:
>             understandfun( _curmsg )

yield _curmsg

is an interesting synonym: the static interface. Compare signatures:

def _draingen( conn ):
...
yield _curmsg

def _draintilclose( sck, th ):
for x in _draingen( sck ):
th._print( x )

or in place of th._print, whatever function you passed to
understandfun.

The general case, pseudocode, consumer:

def _draintilclose( conn, visitee ):
for x in _draingen( conn ):
x.visit( visitee )

class Tool1Visitee:
def typeA( self, arg ):
tool1.listbox.add( arg )
def typeB( self, arg ):
tool1.combobox.add( arg )
class Tool2Visitee:
def typeA( self, arg ):
tool2.icons.add( arg )
def typeB( self, arg ):
tool2.listbox.add( arg )

and:

class VisitorX:
def visit( self, v ):
v.typeA( self._attr ):
class VisitorY:
def visit( self, v ):
v.typeB( self._attr ):

Producer:

def _draingen( conn ):
...
if:
_curvisitee= VisitorX( _curmsg )
else:
_curvisitee= VisitorY( _curmsg )
yield _curvisitee

and:

th1Visitee= Tool1Visitee()
new_thread( _draintilclose, conn1, th1Visitee )
th2Visitee= Tool2Visitee()
new_thread( _draintilclose, conn2, th2Visitee )

Tool1Visitor.typeA still just receives a byte array, but type
information comes with. By that point, the 'message knows' that it's
in a Tool1Visitor, and that it's a typeA message, and so adds it to
tool1.listbox. That puts just the right information in just the right
place... if it's the right time. Or pass the visitee directly to
_draingen as fit, without the visitor or yields, as a callback
collection.

def _draingen( conn, visitee ):
...
if:
visitee.typeA( _curmsg )
else:
visitee.typeB( _curmsg )


As a callback collection:

tool1_ft= dict( typeA= tool1.listbox.add, typeB=
tool1.combobox.add )
tool2_ft= dict( typeA= tool2.icons.add, typeB= too2.listbox.add )
@new_threads( conn1, tool1_ft )
@new_threads( conn2, tool2_ft )
def _( conn, tool_ft ):
for tp, msg in _draingen( conn, tool_ft ):
tool_ft[ tp ]( msg )

using a generator, or directly without:

tool1_ft= dict( typeA= tool1.listbox.add, typeB=
tool1.combobox.add )
new_thread( _draintilclose, conn1, tool1_ft )
tool2_ft= dict( typeA= tool2.icons.add, typeB= too2.listbox.add )
new_thread( _draintilclose, conn2, tool2_ft )

Danny McEvoy

10/18/2011 11:41:00 PM

0

> What are we supposed to do about it? She committed no crimes.

She commited the crime of spitting the fabs up...

Execution! I vote stoning by acorn!

Danny

Danny McEvoy

10/18/2011 11:43:00 PM

0

> Well, I don't think she does that much PR,

What?

and if she does apparently
> it isn't getting noticed outside of the small circle of folks who post
> here.  

What?

Danny

RichL

10/18/2011 11:54:00 PM

0

"Danny McEvoy" <thewalruswasdanny@gmail.com> wrote in message
news:9d875c59-26d0-4a9c-a8ee-e3770859f365@y32g2000yqh.googlegroups.com...
>> What are we supposed to do about it? She committed no crimes.
>
> She commited the crime of spitting the fabs up...

Wow, her mouth must be bigger than I imagined! Or were they in pieces?

Danny McEvoy

10/18/2011 11:55:00 PM

0

> The Fabled Yoko Sycophants!  Love it!

Incidentally it was JL who coined the term sychophants..he used the
term all the time for ppl who just bowed down to him because he was
JL..he hated that..folk who were blind and just kow towed to the
myth..puppets to the myth..JL used the term as he did Beatards.

When Mario turned up at the Flat with May he said "Ah we have an
inventive Beatard!"..then he invited him in..and Mario got to work
with John and May.

Danny

Danny McEvoy

10/18/2011 11:55:00 PM

0

> 1. I really wrote this thread for Danny clowning around.

Me clown around?

Danny

Jeff

10/19/2011 12:06:00 AM

0

On Oct 18, 6:42 pm, Danny McEvoy <thewalruswasda...@gmail.com> wrote:
> > Well, I don't think she does that much PR,
>
> What?
>
> and if she does apparently
>
> > it isn't getting noticed outside of the small circle of folks who post
> > here.  
>
> What?
>
> Danny

Thanks, Danny. I didn't understand any of that myself.

Nil

10/19/2011 12:11:00 AM

0

On 18 Oct 2011, Danny McEvoy <thewalruswasdanny@gmail.com> wrote in
rec.music.beatles:

> Incidentally it was JL who coined the term sychophants..he used the
> term all the time for ppl who just bowed down to him because he was
> JL..he hated that..folk who were blind and just kow towed to the
> myth..puppets to the myth..JL used the term as he did Beatards.

Of course! John Lennon also invented the cupcake, too, you know! But it
wasn't until he met Yoko that together they first devised frosting.


[I believe the term "sycophant" was around for a short while before JL.
Like maybe 1537, according to the ever-reliable Internet.]

Nil

10/19/2011 12:12:00 AM

0

On 18 Oct 2011, Danny McEvoy <thewalruswasdanny@gmail.com> wrote in
rec.music.beatles:

> She commited the crime of spitting the fabs up...

Yes, I remember hearing her do that on "Two Virgins."

BlackMonk

10/19/2011 12:48:00 AM

0

On 10/18/2011 10:36 AM, Fattuchus wrote:

>>
> When I participate in discussions here, I don't necessarily have a
> "goal." It's a discussion group, and the focus is on the Beatles and
> Beatles related things. Yoko clearly is Beatles related, especially
> when she does things relating to Lennon.
>

That doesn't ring true.

You come at these supposed "discussions" with the attitude that you are
bringing "the truth," and that no one could legitimately disagree with
you. Your attitude is that any supposed facts that support your case are
unimpeachable and any that present an alternative to your case are wrong.

I think you showed your true motives when you said "Applause. Nice to
see another poster here sees the light."