Tuesday, 15 May 2012

python - Making a call to a thread object's method from the context of the thread -



python - Making a call to a thread object's method from the context of the thread -

i'm trying create phone call 1 of thread object's methods context of thread using object's handle. however, isn't working , instead phone call method beingness made context of main thread. there way around this?

here's illustration code:

import threading class threadtest(threading.thread): def __init__(self): threading.thread.__init__(self) print '\ninitializing threadtest\n' def call_me(self): ident = threading.current_thread().ident print '\ni called thread ' + str(ident) + '\n' def run(self): ident = threading.current_thread().ident print '\nstarting thread ' + str(ident) + ' threadtest\n' self.call_me() ident = threading.current_thread().ident print '\nmain thread id ' + str(ident) + '\n' tt = threadtest() tt.start() tt.call_me() # illustration output: # main thread id 140735128459616 # # initializing threadtest # # starting thread 4400537600 threadtest # # called thread 4400537600 # # called thread 140735128459616

what i'm looking way create tt.call_me() context of thread such current_thread().ident returns same id when called thread's run method.

any ideas?

python class methods can called thread. goes threading.thread class too. when wrote:

tt.call_me()

you saying, "whatever thread happens running code, please phone call tt.call_me". since in main thread, main thread made call. python doesn't automatically proxy phone call thread.

the self.call_me line in run method worked fine. 'run' method thread , calls in thread.

python python-multithreading

No comments:

Post a Comment