passing argumetns to threading.Thread

S

sashan

I'm trying to pass arguments to my the run method of my class that
inherits from threading.Thread.

class Reader(threading.Thread):
def __init__(self, conn):
threading.Thread.__init__(self, None, None, None, (conn))

def run(self,conn):
while 1:
data = conn.recv(1024)
print data
 
S

sashan

sashan said:
I'm trying to pass arguments to my the run method of my class that
inherits from threading.Thread.

class Reader(threading.Thread):
def __init__(self, conn):
threading.Thread.__init__(self, None, None, None, (conn))

def run(self,conn):
while 1:
data = conn.recv(1024)
print data
woops left out some stuff...

later in the program:

t = Reader(conn)
t.start()
 
S

sashan

sashan said:
woops left out some stuff...

later in the program:

t = Reader(conn)
t.start()
sorry ... i keep hitting the wrong shortcut key combom when sending
these messages resulting in truncations

Anyway after t.start() I get an error message basically saying:
TypeError: 2 arguments expected for Reader.run (1 given)

How do I resolve this?
 
S

sashan

You can, however, use instance variables since Reader is an object:

class Reader(threading.Thread):
def __init__(self, conn):
threading.Thread.__init__(self) # no need for extra args
self.conn = conn

def run(self):
while 1:
data = self.conn.recv(1024)
print data

-Peter

Thanks
 
P

Peter Hansen

sashan said:
I'm trying to pass arguments to my the run method of my class that
inherits from threading.Thread.

class Reader(threading.Thread):
def __init__(self, conn):
threading.Thread.__init__(self, None, None, None, (conn))

def run(self,conn):
while 1:
data = conn.recv(1024)
print data

You can't pass run() arguments.

You can, however, use instance variables since Reader is an object:

class Reader(threading.Thread):
def __init__(self, conn):
threading.Thread.__init__(self) # no need for extra args
self.conn = conn

def run(self):
while 1:
data = self.conn.recv(1024)
print data

-Peter
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top