本文整理汇总了Python中multiprocessing.synchronize.Lock类的典型用法代码示例。如果您正苦于以下问题:Python Lock类的具体用法?Python Lock怎么用?Python Lock使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Lock类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AudioOutputLoop
class AudioOutputLoop(Thread):
def __init__(self, audioDevice, sampleRate = 44100, bufferSize = 512):
Thread.__init__(self, name = "AudioOutputLoop")
self._lock = Lock()
self._isRunning = False
self._audioDevice = audioDevice
self._bufferSize = bufferSize
self._sampleRate = sampleRate
self._maxTimePerBlockInSec = bufferSize / sampleRate
pygame.mixer.init(deviceName = audioDevice.name, frequency = sampleRate, buffer = bufferSize)
self._channel = pygame.mixer.find_channel()
self.testWave = wave.open("./tests/resources/test.wav", "r")
self.totalFrames = self.testWave.getnframes()
self.allsamples = self.testWave.readframes(self.totalFrames)
self._byteArray = bytearray(self._bufferSize * 2)
self.numFrames = 0
logger.info("Mixer initialized")
def terminate(self):
pygame.mixer.quit()
def run(self):
logger.debug("AudioOutputLoop started")
self._isRunning = True
while self._isRunning:
startTime = clock()
self._lock.acquire(True)
# do cool stuff here
if self.numFrames + self._bufferSize > self.totalFrames:
self._isRunning = False
else:
startFrame = self.numFrames
endFrame = startFrame + self._bufferSize * 2
bytesOut = bytes(self.allsamples[startFrame:endFrame])
sound = pygame.mixer.Sound(self.allsamples[startFrame:endFrame])
self._channel.play(sound)
self.numFrames += self._bufferSize * 2
stopTime = clock()
timeInBlockInSec = (stopTime - startTime)
sleepTime = self._maxTimePerBlockInSec - timeInBlockInSec
if sleepTime < 0:
logger.warn("Audio dropout!")
else:
logger.info("CPU: %f", 100 * timeInBlockInSec / self._maxTimePerBlockInSec)
sleep(sleepTime)
self._lock.release()
示例2: __init__
def __init__(self, delegate, pollIntervalInMs = 25):
Thread.__init__(self, name = "MidiEventLoop")
self._lock = Lock()
self._isRunning = False
self.delegate = delegate
self._pollInterval = pollIntervalInMs / 1000
self.devices = None
self.midiMappings = MidiMappingTable(PathFinder.findUserFile('midimappings.sql'))
示例3: __init__
def __init__(self, maxsize=0):
if maxsize <= 0:
maxsize = 2147483647L
self._maxsize = maxsize
self._reader, self._writer = Pipe(duplex=False)
self._rlock = Lock()
self._opid = os.getpid()
if sys.platform == 'win32':
self._wlock = None
else:
self._wlock = Lock()
self._sem = BoundedSemaphore(maxsize)
self._after_fork()
if sys.platform != 'win32':
register_after_fork(self, Queue._after_fork)
示例4: MidiEventLoop
class MidiEventLoop(Thread):
def __init__(self, delegate, pollIntervalInMs = 25):
Thread.__init__(self, name = "MidiEventLoop")
self._lock = Lock()
self._isRunning = False
self.delegate = delegate
self._pollInterval = pollIntervalInMs / 1000
self.devices = None
self.midiMappings = MidiMappingTable(PathFinder.findUserFile('midimappings.sql'))
def quit(self):
self._lock.acquire(True)
if self.devices is not None:
self.devices.closeAll()
self._isRunning = False
self._lock.release()
logger.info("Closed all MIDI devices")
def run(self):
# Initialize is done here so as not to block the main thread
self.devices = MidiDeviceList()
self.devices.openAll()
logger.debug("MidiEventLoop started")
self._isRunning = True
while self._isRunning:
self._lock.acquire(True)
for device in self.devices.openedInputs():
while device.poll():
self._parseEvent(device.readEvents())
self._lock.release()
sleep(self._pollInterval)
pygame.midi.quit()
def _parseEvent(self, eventList):
eventData = eventList[0][0]
midiEvent = MidiEvent(eventData[0], eventData[1], eventData[2], eventList[0][1])
self.midiMappings.process(midiEvent, self.delegate)
示例5: __init__
def __init__(self, audioDevice, sampleRate = 44100, bufferSize = 512):
Thread.__init__(self, name = "AudioOutputLoop")
self._lock = Lock()
self._isRunning = False
self._audioDevice = audioDevice
self._bufferSize = bufferSize
self._sampleRate = sampleRate
self._maxTimePerBlockInSec = bufferSize / sampleRate
pygame.mixer.init(deviceName = audioDevice.name, frequency = sampleRate, buffer = bufferSize)
self._channel = pygame.mixer.find_channel()
self.testWave = wave.open("./tests/resources/test.wav", "r")
self.totalFrames = self.testWave.getnframes()
self.allsamples = self.testWave.readframes(self.totalFrames)
self._byteArray = bytearray(self._bufferSize * 2)
self.numFrames = 0
logger.info("Mixer initialized")
示例6: Queue
class Queue(object):
def __init__(self, maxsize=0):
if maxsize <= 0:
maxsize = _multiprocessing.SemLock.SEM_VALUE_MAX
self._maxsize = maxsize
self._reader, self._writer = Pipe(duplex=False)
self._rlock = Lock()
self._opid = os.getpid()
if sys.platform == 'win32':
self._wlock = None
else:
self._wlock = Lock()
self._sem = BoundedSemaphore(maxsize)
self._after_fork()
if sys.platform != 'win32':
register_after_fork(self, Queue._after_fork)
def __getstate__(self):
assert_spawning(self)
return (self._maxsize, self._reader, self._writer,
self._rlock, self._wlock, self._sem, self._opid)
def __setstate__(self, state):
(self._maxsize, self._reader, self._writer,
self._rlock, self._wlock, self._sem, self._opid) = state
self._after_fork()
def _after_fork(self):
debug('Queue._after_fork()')
self._notempty = threading.Condition(threading.Lock())
self._buffer = collections.deque()
self._thread = None
self._jointhread = None
self._joincancelled = False
self._closed = False
self._close = None
self._send = self._writer.send
self._recv = self._reader.recv
self._poll = self._reader.poll
def put(self, obj, block=True, timeout=None):
assert not self._closed
if not self._sem.acquire(block, timeout):
raise Full
self._notempty.acquire()
try:
if self._thread is None:
self._start_thread()
self._buffer.append(obj)
self._notempty.notify()
finally:
self._notempty.release()
def get(self, block=True, timeout=None):
if block and timeout is None:
self._rlock.acquire()
try:
res = self._recv()
self._sem.release()
return res
finally:
self._rlock.release()
else:
if block:
deadline = time.time() + timeout
if not self._rlock.acquire(block, timeout):
raise Empty
try:
if not self._poll(block and (deadline-time.time()) or 0.0):
raise Empty
res = self._recv()
self._sem.release()
return res
finally:
self._rlock.release()
def qsize(self):
# Raises NotImplementedError on Mac OSX because of broken sem_getvalue()
return self._maxsize - self._sem._semlock._get_value()
def empty(self):
return not self._poll()
def full(self):
return self._sem._semlock._is_zero()
def get_nowait(self):
return self.get(False)
def put_nowait(self, obj):
return self.put(obj, False)
def close(self):
self._closed = True
self._reader.close()
#.........这里部分代码省略.........
示例7: Queue
class Queue(object):
def __init__(self, maxsize = 0):
if maxsize <= 0:
maxsize = _multiprocessing.SemLock.SEM_VALUE_MAX
self._maxsize = maxsize
self._reader, self._writer = Pipe(duplex=False)
self._rlock = Lock()
self._opid = os.getpid()
if sys.platform == 'win32':
self._wlock = None
else:
self._wlock = Lock()
self._sem = BoundedSemaphore(maxsize)
self._after_fork()
if sys.platform != 'win32':
register_after_fork(self, Queue._after_fork)
return
def __getstate__(self):
assert_spawning(self)
return (self._maxsize,
self._reader,
self._writer,
self._rlock,
self._wlock,
self._sem,
self._opid)
def __setstate__(self, state):
self._maxsize, self._reader, self._writer, self._rlock, self._wlock, self._sem, self._opid = state
self._after_fork()
def _after_fork(self):
debug('Queue._after_fork()')
self._notempty = threading.Condition(threading.Lock())
self._buffer = collections.deque()
self._thread = None
self._jointhread = None
self._joincancelled = False
self._closed = False
self._close = None
self._send = self._writer.send
self._recv = self._reader.recv
self._poll = self._reader.poll
return
def put(self, obj, block = True, timeout = None):
if not not self._closed:
raise AssertionError
raise self._sem.acquire(block, timeout) or Full
self._notempty.acquire()
try:
if self._thread is None:
self._start_thread()
self._buffer.append(obj)
self._notempty.notify()
finally:
self._notempty.release()
return
def get(self, block = True, timeout = None):
if block and timeout is None:
self._rlock.acquire()
try:
res = self._recv()
self._sem.release()
return res
finally:
self._rlock.release()
else:
if block:
deadline = time.time() + timeout
if not self._rlock.acquire(block, timeout):
raise Empty
try:
if block:
timeout = deadline - time.time()
if timeout < 0 or not self._poll(timeout):
raise Empty
elif not self._poll():
raise Empty
res = self._recv()
self._sem.release()
return res
finally:
self._rlock.release()
return
def qsize(self):
return self._maxsize - self._sem._semlock._get_value()
def empty(self):
return not self._poll()
def full(self):
return self._sem._semlock._is_zero()
#.........这里部分代码省略.........