python - How to use thread instance function inside a function? -


I was trying to use a thread function in the function. But my terminal says that the global name 'thread1' is not defined?

  import time import threading count = 0 square screen (threading thread): def __init __ (   

self): threading. Thread .__ init __ (self) self.thread_stop = incorrect DRF run (self): while not self. Thread_stop: main () def stop (self): self.thread_stop = true def test (): thread 1 = screen () thread 1. Start (DEF) main (): Global thread 1, count, while true: time. Sleep (1) count = 1 if calculation & gt; = 3: thread1.stop () Print "Stop!"

> test function: thread1

  def test (): global thread 1 ...   

Otherwise, the behavior of the dragon in thread 1 in test is in the form of a local variable, so it is not seen as being started in the main thread1 .

approach (which seems more clear and secure):

  import time import threading count = 0 square screen (threading thread): def __init __ (auto, Count): Threading. Thread .__ init __ (self) self.count = count def do_something (self): while true: time.sleep (1) self.count + = 1 if self.count> = 3: print "stop!" Break def run (self): self.do_something () def test (): thread1 = screen (count) thread1.start () test ()    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -