Threading question

C

codecraig

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.
 
K

Kent Johnson

codecraig said:
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/Cookbook/Python/Recipe/84317

Kent
 
K

Kent Johnson

codecraig said:
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
 
V

Vedanta Barooah

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

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top