access to base class __init__

S

sambo q

I got myself in jam trying to be too fancy with threading.Thread
Docs say / remind to call the base __init__
but I can't fighure out how.

---------------------------
def main()
......
ls.listen(5)
key = ' '
# while key != EXITCHARCTER:
while stop_serving == False:
cs, raddr = ls.accept()
print "Main_Thread: ",cs, raddr
nt = client_socket_handler( cs, raddr )
print threading.enumerate()
key = getkey()

# ls.close()
time.sleep(4)
print "Server Exiting."

class client_socket_handler(threading.Thread):
def __init__(self, cs, remote_address):
???????????????????????
self.threading.Thread.__init__(self,self.socket_handler,None,None)
self.socket = cs
self.rhost_addr = remote_address
print "client_socket_handler.__init__(): ", self.socket,
self.rhost_addr
# t1 = threading.Thread( None,self.socket_handler, None, (5,78) )
# t1.start()
self.start()
print "client_socket_handler.__init__(): ", self.socket,
self.rhost_addr
print "client_socket_handler.__init__(): enumerate()",
threading.enumerate()

def socket_handler( self, invar, indict ):
threadname = self.getName()
print "\"%s started\"" % threadname
print "client_socket_handler.socket_handler() invar: ", invar
instr = self.socket.recv( 500 )
# print instr
req_lines = string.split( instr, "\r" )
for line in req_lines:
line.strip( "\n")
print req_lines
print len( instr )
 
C

castironpi

I got myself in jam trying to be too fancy with  threading.Thread
Docs say / remind to call the base __init__
but I can't fighure out how.

---------------------------
def main()
.....
    ls.listen(5)
    key = ' '
#    while key != EXITCHARCTER:
    while stop_serving == False:
        cs, raddr = ls.accept()
        print "Main_Thread: ",cs, raddr
        nt = client_socket_handler( cs, raddr )
        print threading.enumerate()
        key = getkey()

#    ls.close()
    time.sleep(4)
    print "Server Exiting."

class client_socket_handler(threading.Thread):
    def __init__(self, cs, remote_address):
???????????????????????
        self.threading.Thread.__init__(self,self.socket_handler,None,None)
        self.socket = cs
        self.rhost_addr = remote_address
        print "client_socket_handler.__init__(): ", self.socket,
self.rhost_addr
#        t1 = threading.Thread( None,self.socket_handler, None, (5,78) )
#        t1.start()
        self.start()
        print "client_socket_handler.__init__(): ", self.socket,
self.rhost_addr
        print "client_socket_handler.__init__(): enumerate()",
threading.enumerate()

    def socket_handler( self, invar, indict ):
        threadname = self.getName()
        print "\"%s started\"" % threadname
        print "client_socket_handler.socket_handler() invar: ", invar
        instr = self.socket.recv( 500 )
#        print instr
        req_lines = string.split( instr, "\r" )
        for line in req_lines:
            line.strip( "\n")
        print req_lines
        print len( instr )

----------------------------------

self.threading.Thread.__init__()
self.Thread.__init__()
??

recall a= A() --> a.b() --> A.b( a ). What is A? threading.Thread.

In other words, threading.Thread.__init__( *stuff ).
 
C

castironpi

I got myself in jam trying to be too fancy with  threading.Thread
Docs say / remind to call the base __init__
but I can't fighure out how.

---------------------------
def main()
.....
    ls.listen(5)
    key = ' '
#    while key != EXITCHARCTER:
    while stop_serving == False:
        cs, raddr = ls.accept()
        print "Main_Thread: ",cs, raddr
        nt = client_socket_handler( cs, raddr )
        print threading.enumerate()
        key = getkey()

#    ls.close()
    time.sleep(4)
    print "Server Exiting."

class client_socket_handler(threading.Thread):
    def __init__(self, cs, remote_address):
???????????????????????
        self.threading.Thread.__init__(self,self.socket_handler,None,None)
        self.socket = cs
        self.rhost_addr = remote_address
        print "client_socket_handler.__init__(): ", self.socket,
self.rhost_addr
#        t1 = threading.Thread( None,self.socket_handler, None, (5,78) )
#        t1.start()
        self.start()
        print "client_socket_handler.__init__(): ", self.socket,
self.rhost_addr
        print "client_socket_handler.__init__(): enumerate()",
threading.enumerate()

    def socket_handler( self, invar, indict ):
        threadname = self.getName()
        print "\"%s started\"" % threadname
        print "client_socket_handler.socket_handler() invar: ", invar
        instr = self.socket.recv( 500 )
#        print instr
        req_lines = string.split( instr, "\r" )
        for line in req_lines:
            line.strip( "\n")
        print req_lines
        print len( instr )

----------------------------------

self.threading.Thread.__init__()
self.Thread.__init__()
??

recall a= A() --> a.b() --> A.b( a ). What is A? threading.Thread.

In other words, threading.Thread.__init__( *stuff ).
 
D

Dennis Lee Bieber

I got myself in jam trying to be too fancy with threading.Thread
Docs say / remind to call the base __init__
but I can't fighure out how.
You have a number of problems in this attempt...
class client_socket_handler(threading.Thread):
def __init__(self, cs, remote_address):
???????????????????????
self.threading.Thread.__init__(self,self.socket_handler,None,None)

This is asking for a threading attribute that is part of the
instance... you just need the direct module reference. Drop the leading
self.

I'm not sure why you need to subclass from threading.Thread if all
the thread control is being done internal to this class. Subclassing
means being able to do things like:

mySH = client_socket_handler(some, args)
....
mySh.start()

.... ie; instances of your class behave just like an extended instance of
the Thread class; but the user of this class never sees that it IS a
thread. I believe the other facet is that when subclassing from Thread,
one overloads the run() method AS the target instead of passing some
other function as a target argument and letting the parent run() method
call it.


So why not just make the thread an attribute of the class instead.

self.socket = cs
self.rhost_addr = remote_address
print "client_socket_handler.__init__(): ", self.socket,
self.rhost_addr
# t1 = threading.Thread( None,self.socket_handler, None, (5,78) )
# t1.start()

Which is about what you were doing here, but should have used
self.t1 =...
self.t1.start()
self.start()
print "client_socket_handler.__init__(): ", self.socket,
self.rhost_addr
print "client_socket_handler.__init__(): enumerate()",
threading.enumerate()

def socket_handler( self, invar, indict ):

You aren't passing any arguments to this method.
threadname = self.getName()
print "\"%s started\"" % threadname
print "client_socket_handler.socket_handler() invar: ", invar
instr = self.socket.recv( 500 )
# print instr
req_lines = string.split( instr, "\r" )
for line in req_lines:
line.strip( "\n")

What do you expect the behavior to be if a new-line is embedded and
not adjacent to a carriage return?
print req_lines
print len( instr )

----------------------------------

self.threading.Thread.__init__()
self.Thread.__init__()
??
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Sam

Dennis said:
I'm not sure why you need to subclass from threading.Thread if all
the thread control is being done internal to this class. Subclassing
means being able to do things like:
Well it was the first thing that occured to me how to have
private data for each thread ( IP, PORT , CURDIR if I decide to serve
files. ) although I did come across some private_data CLASS in the docs.

mySH = client_socket_handler(some, args)
...
mySh.start()

hope I can call self.start() from __init__() or self.socket_handler()
... ie; instances of your class behave just like an extended instance of
the Thread class; but the user of this class never sees that it IS a
thread. I believe the other facet is that when subclassing from Thread,
one overloads the run() method AS the target instead of passing some
other function as a target argument and letting the parent run() method
call it.


So why not just make the thread an attribute of the class instead.
Maybe that would be clearer. Starting to wonder if I could return thread
name this way, then again, I think part reason for this aproach was the
fact that once started it could finish/crash all on it's lonesome and
it's instance could wait for garbage collection.
Which is about what you were doing here, but should have used
self.t1 =...
self.t1.start()
Well this shouldn't realy be here just stuff I pasted over.
Which brings me to a question how to start my thread internaly.
Once I managed to call __init__() I should be able to call
self.start() nicht var? or is it vahr?
You aren't passing any arguments to this method.


What do you expect the behavior to be if a new-line is embedded and
not adjacent to a carriage return?
oops:
#handle macs
self.socket.send( "500 Inferior systems not permited.\r\n" )
self.socket.close()

Heh. Besides I don't think NL is permisible as line separator.
the command is at the beginnign and I don't expect to support
any multitude of tags.
 
D

Dennis Lee Bieber

Maybe that would be clearer. Starting to wonder if I could return thread
name this way, then again, I think part reason for this aproach was the
fact that once started it could finish/crash all on it's lonesome and
it's instance could wait for garbage collection.

Well this shouldn't realy be here just stuff I pasted over.
Which brings me to a question how to start my thread internaly.
Once I managed to call __init__() I should be able to call
self.start() nicht var? or is it vahr?

If the end-user isn't supposed to even know there is a thread
running...

class whatever(object):
def __init__(self, *args): #fit whatever args you need
self._myThread = threading.Thread(target=self._something,
args=(what, ever))
self.otherStuff = ...
self._myThread.start()
def _something(w, e):
#the function that should run as the thread worker

.... the actual thread becomes just an attribute...
Heh. Besides I don't think NL is permisible as line separator.
the command is at the beginnign and I don't expect to support
any multitude of tags.

I was thinking something that might have been embedded in the data
itself, once you get to that point.

Consider the results of:
sample="""line 1\r\nline 2\r\nembedded\nline\r\nsomething\r\n"""
sample.split() ['line', '1', 'line', '2', 'embedded', 'line', 'something']
sample.split("\r\n") ['line 1', 'line 2', 'embedded\nline', 'something', '']
sample.split("\r") ['line 1', '\nline 2', '\nembedded\nline', '\nsomething', '\n']
sample.splitlines() ['line 1', 'line 2', 'embedded', 'line', 'something']
sample="""line 1\rline 2\rembedded\nline\rsomething\r"""
sample.splitlines() ['line 1', 'line 2', 'embedded', 'line', 'something']

Note that use "\r\n" as the split argument keeps embedded \n, but
doesn't need a subsequent strip to remove the \n left when using just
"\r" to split. [It does, however, give an empty element following the
concluding "\r\n" but so would "\r" followed by strip]

splitlines() handles \r\n, \n, and \r as line endings -- and no stray
empty ending! (however, \n\r /does/ create an empty element)



--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Sam

['line 1', 'line 2', 'embedded', 'line', 'something']
sample="""line 1\rline 2\rembedded\nline\rsomething\r"""
sample.splitlines() ['line 1', 'line 2', 'embedded', 'line', 'something']
You are right partly right. That code only parsed the HTML
request header which I believe had one specific line termination
as prescribed by RFC ( but I will have too look it up later
since I don't remember whichfor sure. )

Once again got confused on them buggers, "\n" is LINUX
( the least logical).
splitlines() handles \r\n, \n, and \r as line endings -- and no stray
empty ending! (however, \n\r /does/ create an empty element)


!!~! SPLITLINES() BEAUTY, Thanks for reminding me. I mush have a faulty
memory cell or entire row.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top