import stackless class TaskLock(stackless.channel): def __init__(self): self._islock = False self.tasklet = None def lock(self): if self._islock == True : self.receive() self.tasklet = stackless.getcurrent() self._islock = True def unlock(self): self.tasklet = None self._islock = False if self.balance < 0 : self.send(0) lock = TaskLock() lock.lock() ... lock.unlock()