Threading question

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

    #1

    Threading question

    If i have a class such as

    class Foo:
    def doSomething(sel f, cmd, cmd2, callback):
    cmd = <translate command>
    results = <..do some possibly long task..>
    callback(result s)

    What I am looking to do is have that "long task" done in a separate
    thread...how can I do this?

    I was thinking..

    class MyThread(Thread ):
    def __init__(self, cmd, cmd2, callback):
    self.__cmd = cmd
    self.__cmd2 = cmd2
    self.__callback = callback

    def run(self):
    # do stuff here
    self.__callback (some_results)

    ....anyhow, this just didnt seem good. I felt like i either had to pass
    MyThread a bunch of arguments it needed to run the long task, or i
    would end up passing it a 'master'...but then in MyThread I'd have

    self.__master._ _service.execut e_cmd(self.__cm d)

    ....in java I would just create an inner class..but i am not sure
    how/what to do in python.

    thanks.

  • Kent Johnson

    #2
    Re: Threading question

    codecraig wrote:[color=blue]
    > If i have a class such as
    >
    > class Foo:
    > def doSomething(sel f, cmd, cmd2, callback):
    > cmd = <translate command>
    > results = <..do some possibly long task..>
    > callback(result s)
    >
    > What I am looking to do is have that "long task" done in a separate
    > thread...how can I do this?[/color]

    import threading and add this method to class Foo:

    def doSomethingThre aded(self, cmd, cmd2, callback):
    threading.Threa d(target=self.d oSomething, args=(cmd, cmd2, callback)).star t()


    You also might be interested in this recipe:


    Kent

    Comment

    • codecraig

      #3
      Re: Threading question

      Kent,
      thanks for the idea of doSomethingThre aded....that's really cool!

      thanks

      Comment

      • Kent Johnson

        #4
        Re: Threading question

        codecraig wrote:[color=blue]
        > Kent,
        > thanks for the idea of doSomethingThre aded....that's really cool![/color]

        You're welcome, and welcome to Python. I am a convert from Java myself.

        You will find that many things that you would do with inner classes in Java, you can do much more
        directly with function references in Python. First class functions are your friends :-)

        Kent

        Comment

        • Vedanta Barooah

          #5
          Re: Threading question

          there is a pwthonw.exe for windows python installations which does not
          spawn a console window... is that what you are looking for ;)


          On 4/23/05, Kent Johnson <kent37@tds.net > wrote:[color=blue]
          > codecraig wrote:[color=green]
          > > Kent,
          > > thanks for the idea of doSomethingThre aded....that's really cool![/color]
          >
          > You're welcome, and welcome to Python. I am a convert from Java myself.
          >
          > You will find that many things that you would do with inner classes in Java, you can do much more
          > directly with function references in Python. First class functions are your friends :-)
          >
          > Kent
          > --
          > http://mail.python.org/mailman/listinfo/python-list
          > [/color]


          --
          *~:~*~:~*~:~*~: ~*~:~*~:~*~:~*~ :~*~:~*~:~*
          Vedanta Barooah
          YM! - vedanta2006
          Skype - vedanta2006

          Comment

          Working...