passing argumetns to threading.Thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sashan

    passing argumetns to threading.Thread

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

    class Reader(threadin g.Thread):
    def __init__(self, conn):
    threading.Threa d.__init__(self , None, None, None, (conn))

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

  • sashan

    #2
    Re: passing argumetns to threading.Threa d

    sashan wrote:[color=blue]
    > I'm trying to pass arguments to my the run method of my class that
    > inherits from threading.Threa d.
    >
    > class Reader(threadin g.Thread):
    > def __init__(self, conn):
    > threading.Threa d.__init__(self , None, None, None, (conn))
    >
    > def run(self,conn):
    > while 1:
    > data = conn.recv(1024)
    > print data
    >[/color]
    woops left out some stuff...

    later in the program:

    t = Reader(conn)
    t.start()

    Comment

    • sashan

      #3
      Re: passing argumetns to threading.Threa d

      sashan wrote:[color=blue]
      > sashan wrote:
      >[color=green]
      >> I'm trying to pass arguments to my the run method of my class that
      >> inherits from threading.Threa d.
      >>
      >> class Reader(threadin g.Thread):
      >> def __init__(self, conn):
      >> threading.Threa d.__init__(self , None, None, None, (conn))
      >> def run(self,conn): while 1:
      >> data = conn.recv(1024)
      >> print data
      >>[/color]
      > woops left out some stuff...
      >
      > later in the program:
      >
      > t = Reader(conn)
      > t.start()
      >[/color]
      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?


      Comment

      • Peter Hansen

        #4
        Re: passing argumetns to threading.Threa d

        sashan wrote:[color=blue]
        >
        > I'm trying to pass arguments to my the run method of my class that
        > inherits from threading.Threa d.
        >
        > class Reader(threadin g.Thread):
        > def __init__(self, conn):
        > threading.Threa d.__init__(self , None, None, None, (conn))
        >
        > def run(self,conn):
        > while 1:
        > data = conn.recv(1024)
        > print data[/color]

        You can't pass run() arguments.

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

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

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

        -Peter

        Comment

        • sashan

          #5
          Re: passing argumetns to threading.Threa d

          [color=blue]
          > You can, however, use instance variables since Reader is an object:
          >
          > class Reader(threadin g.Thread):
          > def __init__(self, conn):
          > threading.Threa d.__init__(self ) # no need for extra args
          > self.conn = conn
          >
          > def run(self):
          > while 1:
          > data = self.conn.recv( 1024)
          > print data
          >
          > -Peter[/color]

          Thanks

          Comment

          Working...