Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from threading import Lock
- class NonBlockingMonitor:
- lock = Lock()
- def __init__(self):
- pass
- def do(self, method):
- """This method does not wait on the resource. If it's available it does
- the action and returns True,
- otherwise it returns False without doing anything.
- Params:
- method the method that must be called if no other thread
- managed to acquire the lock."""
- if self.lock.acquire(False): # False for non blocking
- ret = method()
- self.lock.release()
- return ret
- else:
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement