I am trying to debug a multi-threaded Python application which uses different locks
Log instead of place. Debug (...) To track all the shots where and when the locks are acquired and released, threading.Lock.acquire () And threading.Lock.release () , and the prefix of their greeting with something similar to the following: To discuss where log is some global logging object - Now ideally, the log entry should be derived on the name "lock" respectively, so that without lock object these methods will be applied to the lock, its name will be output, operation is decorated, current Thread, class, and work in which the operation (acquisition | release) is called. Disclaimer : I believe the code above will not be enough for any decorator implementation. It is only provided to give me the taste of what I think can be achieved. Do anyone know if I can do the threading library to decorate the standard library methods without having to do the original source code, that is, by my calling application code? Maybe I am barking the wrong tree and there is a better way of achieving one end without using decorators? Many thanks in advance for any guidance if this is actually the case Solution: (induced by lazy) logs the following code lock operations And gives me the name of the method / function of the lock operation call (I statuses and the additional wait () and notify () Optimizing the code to work with methods: The log image for the log lock is so much. init was defined with logging. Format: to give me the calling thread as follows: The problem was that I have automatically set my logger to log the name of thread, like in the answer I found out that this subclass was not possible for the lock, so I had to wrap it like this: I have logged an ID of the object, so I I can distinguish between many locks, you do not need it.
log .debug ("lock :: acquire () [% S.% S.% S] "% (current thread (). GetName (), own______________________________________________________________________________________________?
# Lock and lock the class To make the entry of the use class loglock (object) class: "" "A standard lock wraps up, so that an attempt to use the lock according to its API is logged for debugging purposes" def __init__ (self, Name, log): self.name = str (name) self.log = logs self.lock = threading.Lock () self.log.debug ("{0} created {1}". Format (Observe Stack () [1] [3], self.name) def (self, blocked = true): self.log.debug ("{0} trying to achieve {1}" format (inspect.stack () [1] [3], self.name)) K = self.lock.acquire (blocked) if ret == true: self.log.debug ( "{0} acquisition {1}". Format (inspection (.) [1] [3], self. Name) Other: self.log.debug ("{0} non-block {1} lock kick kickoff failure" format (inspection stock) 1] [3], name itself) write def release (self): self.log.debug ("{0} release {1}". Format () (1) [1] [3], name itself. )) Self.lock.release () def __enter __ (self): self.acquire () def __exit __ (self, exc_type, exc_val, exc_tb): self.release () return false false exceptions do not swallow
# log_format = \ logging.Formatter ('% (asctime) s% (levelname) s% (Thread name) s% (message) s')
class loglock (object): def __init __ (auto, name): self.name = Str (name) self.lock = lock (def) (self, blocked = true): log.debug ("{0: x} {1} trying to get lock" format (ID (self) , Self.name)) Rate = self.lock.acquire (blocked) if ret == true: log.debug ("{0: x} acquired {1} lock" format (id (self), self.name) ) Other: Log Debug ("{0: x} {1} blocking without lock and takeover Fruit ". Format (ID (own), self.name)) writ Def release (self): log. Debug ("{0: x} {1} release the lock" format. (Id (own), self. Name)) self.lock.release () def __enter __ (self): self.acquire () def __exit __ (self, exc_type, exc_val, exc_tb): self .release () Returns wrong # Do not swallow exceptions
Comments
Post a Comment