def __init__(self):
self.help_msg = \
u"H: ????\n" \
u"L: ???????\n" \
u"M: ????\n" \
u"N: ???\n"\
u"U: ????\n"\
u"R: ????\n"\
u"S: ????\n"\
u"T: ????\n"\
u"G: ????\n"\
u"E: ??\n"
self.con = threading.Condition()
self.myNetease = MyNetease()
self.playlist = self.myNetease.get_top_songlist() #???????
self.mp3 = None
t = threading.Thread(target=self.play)
t.start()
python类Condition()的实例源码
def test_thread_safe(self):
thread_count = 4
repeats = 1000
latch = [threading.Condition(), thread_count]
def thread_main(latch):
for _ in range(repeats):
self.assertEqual("False", self.conn.test_service.bool_to_string(False))
self.assertEqual(12345, self.conn.test_service.string_to_int32("12345"))
with latch[0]:
latch[1] -= 1
if latch[1] <= 0:
latch[0].notifyAll()
for i in range(thread_count):
t = threading.Thread(target=thread_main, args=(latch,))
t.daemon = True
t.start()
with latch[0]:
while latch[1] > 0:
latch[0].wait(10)
self.assertEqual(0, latch[1]);
def __init__(self, maxsize=0):
self.maxsize = maxsize
self._init(maxsize)
# mutex must be held whenever the queue is mutating. All methods
# that acquire mutex must release it before returning. mutex
# is shared between the three conditions, so acquiring and
# releasing the conditions also acquires and releases mutex.
self.mutex = _threading.Lock()
# Notify not_empty whenever an item is added to the queue; a
# thread waiting to get is notified then.
self.not_empty = _threading.Condition(self.mutex)
# Notify not_full whenever an item is removed from the queue;
# a thread waiting to put is notified then.
self.not_full = _threading.Condition(self.mutex)
# Notify all_tasks_done whenever the number of unfinished tasks
# drops to zero; thread waiting to join() is notified to resume
self.all_tasks_done = _threading.Condition(self.mutex)
self.unfinished_tasks = 0
def __init__(self):
self.message = None
self.__event = threading.Event()
self.__cond = threading.Condition()
self.__mail_queue = Queue(100)
def __init__(self, queue, counter):
self.queue = queue
self.counter = counter
self.checkers = {}
self.queues = {}
self.events = {}
self.line = None
self.__event = threading.Event()
self.__cond = threading.Condition()
def __init__(self, input_stream=None, porttype=None, rem_file=False):
"""
Instantiates a new object responsible for writing data from the port
into a file. The file name is given by the input_stream variable.
It is important to notice that the porttype is a BULKIO__POA type and
not a BULKIO type. The reason is because it is used to generate a
Port class that will be returned when the getPort() is invoked. The
returned class is the one acting as a server and therefore must be a
Portable Object Adapter rather and a simple BULKIO object.
Inputs:
<input_stream> The X-Midas file to generate
<porttype> The BULKIO__POA data type
<rem_file> Removes the input_stream if present
"""
if input_stream != None and os.path.isfile(input_stream):
os.remove(input_stream)
self.port_type = porttype
self.outFile = input_stream
self.port_lock = threading.Lock()
self.eos_cond = threading.Condition(self.port_lock)
self.gotEOS = False
self.header = None
self.done = False
self._firstPacket = True
def waitAllPacketsSent(self, timeout=None):
"""
Wait until all of the packets queued on this source have been pushed to
all connected ports. If timeout is given, it should be the maximum
number of seconds to wait before giving up.
"""
self._packetsSentCond.acquire()
try:
# Assume no spurious signals will occur, so we can defer to the
# timeout handling of Python's Condition object.
if self._packetsPending > 0:
self._packetsSentCond.wait(timeout)
finally:
self._packetsSentCond.release()
def __init__(self):
self._recv_disconnect = True
self.logger = logging.getLogger("ossie.events.Subscriber.Receiver")
self._lock = threading.Lock()
self._cond = threading.Condition(self._lock)
def __init__(self, name, logger=None, sriCompare=sri.compare, newSriCallback=None, sriChangeCallback=None, maxsize=100, PortTransferType=_TYPE_ ):
self.name = name
self.logger = logger
self.queue = collections.deque()
self._maxSize = maxsize
self.port_lock = threading.Lock()
self._not_full = threading.Condition(self.port_lock)
self._not_empty = threading.Condition(self.port_lock)
self._breakBlock = False
self.stats = InStats(name, PortTransferType)
self.blocking = False
self.sri_cmp = sriCompare
self.newSriCallback = newSriCallback
self.sriChangeCallback = sriChangeCallback
self.sriDict = {} # key=streamID, value=StreamSRI
if logger==None:
self.logger = logging.getLogger("redhawk.bulkio.input."+name)
_cmpMsg = "DEFAULT"
_newSriMsg = "EMPTY"
_sriChangeMsg = "EMPTY"
if sriCompare != sri.compare:
_cmpMsg = "USER_DEFINED"
if newSriCallback:
_newSriMsg = "USER_DEFINED"
if sriChangeCallback:
_sriChangeMsg = "USER_DEFINED"
if self.logger:
self.logger.debug( "bulkio::InPort CTOR port:" + str(name) +
" Blocking/MaxInputQueueSize " + str(self.blocking) + "/" + str(maxsize) +
" SriCompare/NewSriCallback/SriChangeCallback " + _cmpMsg + "/" + _newSriMsg + "/" + _sriChangeMsg );
def __init__(self, name, logger=None, sriCompare=sri.compare, newSriCallback=None, sriChangeCallback=None, maxsize=100, PortTransferType=_TYPE_ ):
self.name = name
self.logger = logger
self.queue = collections.deque()
self._maxSize = maxsize
self.port_lock = threading.Lock()
self._not_full = threading.Condition(self.port_lock)
self._not_empty = threading.Condition(self.port_lock)
self._breakBlock = False
self.stats = InStats(name, PortTransferType)
self.blocking = False
self.sri_cmp = sriCompare
self.newSriCallback = newSriCallback
self.sriChangeCallback = sriChangeCallback
self.sriDict = {} # key=streamID, value=StreamSRI
if logger==None:
self.logger = logging.getLogger("redhawk.bulkio.input."+name)
_cmpMsg = "DEFAULT"
_newSriMsg = "EMPTY"
_sriChangeMsg = "EMPTY"
if sriCompare != sri.compare:
_cmpMsg = "USER_DEFINED"
if newSriCallback:
_newSriMsg = "USER_DEFINED"
if sriChangeCallback:
_sriChangeMsg = "USER_DEFINED"
if self.logger:
self.logger.debug( "bulkio::InPort CTOR port:" + str(name) +
" Blocking/MaxInputQueueSize " + str(self.blocking) + "/" + str(maxsize) +
" SriCompare/NewSriCallback/SriChangeCallback " + _cmpMsg + "/" + _newSriMsg + "/" + _sriChangeMsg );
def __init__(self, proxy=None):
self.connected = False
self._proxy = proxy
self._recreate_socket()
# Support for multi-threading advantages and safety
self.cancelled = Event() # Has the read operation been cancelled?
self.delay = 0.1 # Read delay when there was no data available
self._lock = Lock()
self._buffer = []
self._read_thread = Thread(target=self._reading_thread, daemon=True)
self._cv = Condition() # Condition Variable
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 __init__(self, maxsize=0):
Queue.__init__(self, maxsize)
self._unfinished_tasks = Semaphore(0)
self._cond = Condition()
def __init__(self, cache, callback):
self._cond = threading.Condition(threading.Lock())
self._job = job_counter.next()
self._cache = cache
self._ready = False
self._callback = callback
cache[self._job] = self
def __init__(self, cache):
self._cond = threading.Condition(threading.Lock())
self._job = job_counter.next()
self._cache = cache
self._items = collections.deque()
self._index = 0
self._length = None
self._unsorted = {}
cache[self._job] = self
def __init__(self, parties):
"""Create a barrier, initialised to 'parties' threads."""
self.cond = threading.Condition(threading.Lock())
self.parties = parties
# Indicates the number of waiting parties.
self.waiting = 0
# generation is needed to deal with spurious wakeups. If self.cond.wait()
# wakes up for other reasons, generation will force it go back to wait().
self.generation = 0
self.broken = False
def __init__(self, value=1):
if value < 0:
raise ValueError("semaphore initial value must be >= 0")
self._cond = threading.Condition(threading.Lock())
self._value = value
def __init__(self, show_state_steps, show_dynamic_steps):
super(Monitor, self).__init__()
self._processor = SaltEventProcessor()
self._processor.add_listener(Monitor.DeepSeaEventListener(self))
self._show_state_steps = show_state_steps
self._show_dynamic_steps = show_dynamic_steps
self._running_stage = None
self._monitor_listeners = []
self._event_lock = threading.Lock()
self._event_cond = threading.Condition(self._event_lock)
self._event_buffer = []
self._running = False
self._stage_steps = {}
def __init__(self, session, statements_and_params):
self.session = session
self._enum_statements = enumerate(iter(statements_and_params))
self._condition = Condition()
self._fail_fast = False
self._results_queue = []
self._current = 0
self._exec_count = 0
self._exec_depth = 0
def __init__(self, conn):
super(MultiplexedInputStream, self).__init__(conn)
# Arbitrates access to this InputStream (it's used simultaneously
# by a Request and its owning Connection object).
lock = threading.RLock()
# Notifies Request thread that there is new data available.
self._lock = threading.Condition(lock)