python - Creating a new thread that _really_ runs in parallel -
i have following code in class:
def persistdbthread(self): while true: thread(target=self.__persistdb) time.sleep(10) def __persistdb(self): open(self.authdbpath, 'w') outfile: json.dump(self.authdb, outfile)
the thread gets started in __ main__ start thread blocking in main execution.
why this? know gil - it's in same process. task switching happens in same process in micro-thread, why doesn't switch back?
thanks!
sorry asking:
def persistdbthread(self): thread(target=self.__persistdb).start() def __persistdb(self): while true: time.sleep(10) outfile = open(self.authdbpath, 'w') json.dump(self.authdb, outfile)
you calling __persistdb
soon. use target=self.__persistdb
no parentheses @ end. when include parentheses, calling function before threading call made. without parentheses, pass function argument called later.
you need call resulting thread
object's start
method. described in the documentation, should read.
also, don't run in while true
loop. create endless numbers of threads call thread
again , again. google or search stackoverflow "python threading example" find many examples of right way use threading
module, e.g., here.
Comments
Post a Comment