简体   繁体   中英

python simple thread not working

I'm trying to use a new thread or multiprocessing to run a function.

The function is called like this:

Actualize_files_and_folders(self)

i've read alot about multiprocessing and threads, and looking the questions at StackOverflow but i can't make it work... It would be great to have some help >.<

im calling the function with a button.

def on_button_act_clicked(self, menuitem, data=None):

     self.window_waiting.show()

     Actualize_files_and_folders(self)

     self.window_waiting.hide()

In the waiting_window i have a button called 'cancel', it would be great if i can have a command/function that kills the thread.

i've tryed a lot of stuff, for exemple:

self.window_waiting.show()
from multiprocessing import Process
a=Process(Target=Actualize_files_and_folders(self))
a.start()
a.join()
self.window_waiting.hide()

But the window still freezing, and window_waiting is displayed at the end of Actualize_files_and_folders(self), like if i had called a normal function.

Thanks so much for any help!!

It looks like the worker function is being called rather than used as a callback for the process target:

process = Process(target=actualize_files_and_folders(self))

This is essentially equivalent to:

tmp = actualize_files_and_folders(self)
process = Process(target=tmp)

So the worker function is called in the main thread blocking it. The result of this function is passed into the Process as the target which will do nothing if it is None. You need to pass the function itself as a callback, not its result:

process = Process(target=actualize_files_and_folders, args=[self])
process.start()

See: https://docs.python.org/2/library/multiprocessing.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM