def _worker_func(self):
        # Amount of audio data "buffered" (in secs)
        buffered_time = 0.

        self._lock.acquire()
        self._worker_timestamp = 0.0
        self._lock.release()

        while True:
            self._lock.acquire()

            # Use up "buffered" audio based on amount of time passed.
            timestamp = self.get_time()
            buffered_time -= timestamp - self._worker_timestamp
            self._worker_timestamp = timestamp

            if _debug:
                print 'timestamp: %f' % timestamp

            # Dispatch events
            events = self._events # local var ok within this lock
            while events and events[0].timestamp <= timestamp:
                events[0]._sync_dispatch_to_player(self.player)
                del events[0]

            if events:
                next_event_timestamp = events[0].timestamp
            else:
                next_event_timestamp = None

            self._lock.release()

            # Calculate how much data to request from source
            secs = self._buffer_time - buffered_time
            bytes = secs * self.source_group.audio_format.bytes_per_second
            if _debug:
                print 'need to get %d bytes (%f secs)' % (bytes, secs)

            # No need to get data, sleep until next event or buffer update
            # time instead.
            if bytes < self._min_update_bytes:
                sleep_time = buffered_time / 2
                if next_event_timestamp is not None:
                    sleep_time = min(sleep_time, 
                                     next_event_timestamp - timestamp)
                if _debug:
                    print 'sleeping for %f' % sleep_time
                time.sleep(sleep_time)
                continue

            # Pull audio data from source
            audio_data = self.source_group.get_audio_data(int(bytes))
            if not audio_data:
                mt_media.MediaEvent(timestamp,
                    'on_source_group_eos')._sync_dispatch_to_player(self.player)
                break

            # Pretend to buffer audio data, collect events.
            buffered_time += audio_data.duration
            self._lock.acquire()
            self._events.extend(audio_data.events)
            self._lock.release()

            if _debug:
                print 'got %s secs of audio data' % audio_data.duration
                print 'now buffered to %f' % buffered_time
                print 'events: %r' % events
dataset/ETHPy150Open ardekantur/pyglet/experimental/mt_media/drivers/silent.py/SilentAudioPlayer._worker_func/original