By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Threading question

P: n/a
If i have a class such as

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

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.execute_cmd(self.__cmd)

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

thanks.

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
codecraig wrote:
If i have a class such as

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

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


import threading and add this method to class Foo:

def doSomethingThreaded(self, cmd, cmd2, callback):
threading.Thread(target=self.doSomething, args=(cmd, cmd2, callback)).start()
You also might be interested in this recipe:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/84317

Kent
Jul 19 '05 #2

P: n/a
Kent,
thanks for the idea of doSomethingThreaded....that's really cool!

thanks

Jul 19 '05 #3

P: n/a
codecraig wrote:
Kent,
thanks for the idea of doSomethingThreaded....that's really cool!


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
Jul 19 '05 #4

P: n/a
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 <ke****@tds.net> wrote:
codecraig wrote:
Kent,
thanks for the idea of doSomethingThreaded....that's really cool!


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

--
*~:~*~:~*~:~*~:~*~:~*~:~*~:~*~:~*~:~*~:~*
Vedanta Barooah
YM! - vedanta2006
Skype - vedanta2006
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.