I assume that you are converting from C ++, where a lot of meaning can be attached to areas of variables equal to the lifetimes of the variables. This does not apply to Python, and the garbage collector in general. Scope! = Lifetime simply because garbage collection occurs whenever the interpreter approaches it, and not along the boundaries of the area. Especially since you are trying to make asynchronous material with it, the raised hair on your neck should vibrate until all the warning bells in your head scream! You can do things with the lifetime of objects using del . (In fact, if you read the sources in the cpython garbage collection module, the obvious (and somewhat ridiculous) contempt for objects with finalizers ( del ) expressed there should tell everyone to use even the object's lifetime only if necessary).
You can use sys.getrefcount (self) to find out when to leave the loop in your thread. But I can hardly recommend it (just try what numbers will return. You will not be happy. To find out who holds what just check gc.get_referrers (self)). The reference count may / will depend on garbage collection.
In addition, linking the execution time of the execution thread to the areas / lifetimes of objects is a 99% error. Even Boost does not. From your RAII method, you can define something called a “disconnected” thread. http://www.boost.org/doc/libs/1_55_0/doc/html/thread/thread_management.html
source share