예제 #1
0
    def acquire(self, blocking=True, timeout=-1):
        # This is the Python 3 signature.
        # On Python 2, Lock.acquire has the signature `Lock.acquire([wait])`
        # where `wait` is a boolean that cannot be passed by name, only position.
        # so we're fine to use the Python 3 signature.

        # Transform the default -1 argument into the None that our
        # semaphore implementation expects, and raise the same error
        # the stdlib implementation does.
        if timeout == -1:
            timeout = None
        if not blocking and timeout is not None:
            raise ValueError("can't specify a timeout for a non-blocking call")
        if timeout is not None:
            if timeout < 0:
                # in C: if(timeout < 0 && timeout != -1)
                raise ValueError("timeout value must be strictly positive")
            if timeout > self._TIMEOUT_MAX:
                raise OverflowError('timeout value is too large')

        acquired = BoundedSemaphore.acquire(self, blocking, timeout)
        if not acquired and not blocking and getcurrent(
        ) is not get_hub_if_exists():
            # Run other callbacks. This makes spin locks works.
            # We can't do this if we're in the hub, which we could easily be:
            # printing the repr of a thread checks its tstate_lock, and sometimes we
            # print reprs in the hub.
            # See https://github.com/gevent/gevent/issues/1464

            # By using sleep() instead of self.wait(0), we don't force a trip
            # around the event loop *unless* we've been running callbacks for
            # longer than our switch interval.
            sleep()
        return acquired
예제 #2
0
파일: lock.py 프로젝트: yalon/gevent
    def acquire(self, blocking=1):
        tid = get_ident()
        gid = id(getcurrent())
        tid_gid = (tid, gid)
        if tid_gid == self._owner:  # We trust the GIL here so we can do this comparison w/o locking.
            self._count = self._count + 1
            return True

        greenlet_lock = self._get_greenlet_lock()

        self._wait_queue.append(gid)
        # this is a safety in case an exception is raised somewhere and we must make sure we're not in the queue
        # otherwise it'll get stuck forever.
        remove_from_queue_on_return = True
        try:
            while True:
                if not greenlet_lock.acquire(blocking):
                    return False  # non-blocking and failed to acquire lock

                if self._wait_queue[0] == gid:
                    # Hurray, we can have the lock.
                    self._owner = tid_gid
                    self._count = 1
                    remove_from_queue_on_return = False  # don't remove us from the queue
                    return True
                else:
                    # we already hold the greenlet lock so obviously the owner is not in our thread.
                    greenlet_lock.release()
                    if blocking:
                        sleep(0.0005)  # 500 us -> initial delay of 1 ms
                    else:
                        return False
        finally:
            if remove_from_queue_on_return:
                self._wait_queue.remove(gid)
예제 #3
0
        def join(timeout=None):
            end = None
            if threading_mod.current_thread() is thread:
                raise RuntimeError("Cannot join current thread")
            if thread_greenlet is not None and thread_greenlet.dead:
                return
            # You may ask: Why not call thread_greenlet.join()?
            # Well, in the one case we actually have a greenlet, it's the
            # low-level greenlet.greenlet object for the main thread, which
            # doesn't have a join method.
            #
            # You may ask: Why not become the main greenlet's *parent*
            # so you can get notified when it finishes? Because you can't
            # create a greenlet cycle (the current greenlet is a descendent
            # of the parent), and nor can you set a greenlet's parent to None,
            # so there can only ever be one greenlet with a parent of None: the main
            # greenlet, the one we need to watch.
            #
            # You may ask: why not swizzle out the problematic lock on the main thread
            # into a gevent friendly lock? Well, the interpreter actually depends on that
            # for the main thread in threading._shutdown; see below.

            if not thread.is_alive():
                return

            if timeout:
                end = time() + timeout

            while thread.is_alive():
                if end is not None and time() > end:
                    return
                sleep(0.01)
예제 #4
0
            def _shutdown():
                # Release anyone trying to join() me,
                # and let us switch to them.
                if not main_thread._tstate_lock:
                    return

                main_thread._tstate_lock.release()
                from gevent import sleep
                try:
                    sleep()
                except:  # pylint:disable=bare-except
                    # A greenlet could have .kill() us
                    # or .throw() to us. I'm the main greenlet,
                    # there's no where else for this to go.
                    from gevent import get_hub
                    get_hub().print_exception(_greenlet, *sys.exc_info())

                # Now, this may have resulted in us getting stopped
                # if some other greenlet actually just ran there.
                # That's not good, we're not supposed to be stopped
                # when we enter _shutdown.
                main_thread._is_stopped = False
                main_thread._tstate_lock = main_thread.__real_tstate_lock
                main_thread.__real_tstate_lock = None
                # The only truly blocking native shutdown lock to
                # acquire should be our own (hopefully), and the call to
                # _stop that orig_shutdown makes will discard it.

                orig_shutdown()
                patch_item(threading_mod, '_shutdown', orig_shutdown)
예제 #5
0
    def test_feed_manager(self):
        """Test attack feed manager"""

        # Create a session manager and the feed manager attached to it
        config = Configuration()
        session_manager = SessionManager(config)
        feed_manager = FeedManager(config, session_manager)
        feed_manager.add_feed(DummyFeed(config))
        feed_manager.run()

        # Create an event
        event = Event("Test event")

        # Obtain a session and add the event
        session = session_manager.get_session("test", "127.0.0.1", 3200,
                                              "127.0.0.1", 3201)
        session.add_event(event)

        # Give the feed manager time for processing the event
        sleep(1)

        # Stop the feed manager and check if the event was processed
        feed_manager.stop()
        new_event = DummyFeed.events.get()
        self.assertIs(event, new_event)
예제 #6
0
 def _set_size(self, size):
     if size < 0:
         raise ValueError('Size of the pool cannot be negative: %r' %
                          (size, ))
     if size > self._maxsize:
         raise ValueError(
             'Size of the pool cannot be bigger than maxsize: %r > %r' %
             (size, self._maxsize))
     if self.manager:
         self.manager.kill()
     while len(self._worker_greenlets) < size:
         self._add_thread()
     delay = self.hub.loop.approx_timer_resolution
     while len(self._worker_greenlets) > size:
         while len(self._worker_greenlets
                   ) - size > self.task_queue.unfinished_tasks:
             self.task_queue.put(None)
         if getcurrent() is self.hub:
             break
         sleep(delay)
         delay = min(delay * 2, .05)
     if self._worker_greenlets:
         self.fork_watcher.start(self._on_fork)
     else:
         self.fork_watcher.stop()
예제 #7
0
 def _adjust_wait(self):
     delay = 0.0001
     while True:
         self._adjust_step()
         if self._size <= self._maxsize:
             return
         sleep(delay)
         delay = min(delay * 2, .05)
예제 #8
0
 def _adjust_wait(self):
     delay = 0.0001
     while True:
         self._adjust_step()
         if len(self._worker_greenlets) <= self._maxsize:
             return
         sleep(delay)
         delay = min(delay * 2, .05)
예제 #9
0
 def _adjust_wait(self):
     delay = 0.0001
     while True:
         self._adjust_step()
         if self._size <= self._maxsize:
             return
         sleep(delay)
         delay = min(delay * 2, .05)
예제 #10
0
 def _adjust_wait(self):
     delay = self.hub.loop.approx_timer_resolution
     while True:
         self._adjust_step()
         if len(self._worker_greenlets) <= self._maxsize:
             return
         sleep(delay)
         delay = min(delay * 2, .05)
예제 #11
0
파일: threadpool.py 프로젝트: HVF/gevent
 def _manage(self, maxsize=None):
     if maxsize is None:
         maxsize = self._maxsize
     delay = 0.0001
     while True:
         self._adjust(maxsize)
         if self._size <= maxsize:
             return
         sleep(delay)
         delay = min(delay * 2, 0.05)
예제 #12
0
파일: subprocess.py 프로젝트: Therp/gevent
 def _internal_poll(self):
     """Check if child process has terminated.  Returns returncode
     attribute.
     """
     if self.returncode is None:
         if get_hub() is not getcurrent():
             sig_pending = getattr(self._loop, 'sig_pending', True)
             if sig_pending:
                 sleep(0.00001)
     return self.returncode
예제 #13
0
    def setUp(self):
        # drive_path = os.path.abspath(".\geckodriver.exe")
        # drive_path = os.path.abspath(".\geckodriver.exe")
        # self.driver.implicitly_wait(2)
        # self.addCleanup(self.driver.quit)

        self.driver = webdriver.Firefox()
        self.driver.maximize_window()
        sleep(3)  # 隐性等待时间为30秒
        self.base_url = "https://www.baidu.com"
예제 #14
0
 def _internal_poll(self):
     """Check if child process has terminated.  Returns returncode
     attribute.
     """
     if self.returncode is None:
         if get_hub() is not getcurrent():
             sig_pending = getattr(self._loop, 'sig_pending', True)
             if sig_pending:
                 sleep(0.00001)
     return self.returncode
예제 #15
0
 def _manage(self, maxsize=None):
     if maxsize is None:
         maxsize = self._maxsize
     delay = 0.0001
     while True:
         self._adjust(maxsize)
         if self._size <= maxsize:
             return
         sleep(delay)
         delay = min(delay * 2, .05)
예제 #16
0
파일: monkey.py 프로젝트: phierus/gevent
 def join(timeout=None):
     if threading.current_thread() is main_thread:
         raise RuntimeError("Cannot join current thread")
     if _greenlet.dead or not main_thread.is_alive():
         return
     elif timeout:
         raise ValueError("Cannot use a timeout to join the main thread")
         # XXX: Make that work
     else:
         while main_thread.is_alive():
             sleep(0.01)
예제 #17
0
def joinall(greenlets, polling_period=0.2):
    """Wait for the greenlets to finish by polling their status"""
    current = 0
    while current < len(greenlets) and greenlets[current].dead:
        current += 1
    delay = 0.002
    while current < len(greenlets):
        delay = min(polling_period, delay * 2)
        sleep(delay)
        while current < len(greenlets) and greenlets[current].dead:
            current += 1
예제 #18
0
 def join(timeout=None):
     if threading.current_thread() is main_thread:
         raise RuntimeError("Cannot join current thread")
     if _greenlet.dead or not main_thread.is_alive():
         return
     elif timeout:
         raise ValueError("Cannot use a timeout to join the main thread")
         # XXX: Make that work
     else:
         while main_thread.is_alive():
             sleep(0.01)
예제 #19
0
def joinall(greenlets, polling_period=0.2):
    """Wait for the greenlets to finish by polling their status"""
    current = 0
    while current < len(greenlets) and greenlets[current].dead:
        current += 1
    delay = 0.002
    while current < len(greenlets):
        delay = min(polling_period, delay * 2)
        sleep(delay)
        while current < len(greenlets) and greenlets[current].dead:
            current += 1
예제 #20
0
    def wait(self, check_interval=0.1):
        # non-blocking, use hub.sleep
        try:
            while True:
                status = self.poll()
                if status is not None:
                    return status
                hub.sleep(check_interval)

        except OSError, e:
            if e.errno == errno.ECHILD:
                # no child process, this happens if the child process
                # already died and has been cleaned up
                return -1
예제 #21
0
    def wait(self, check_interval=0.1):
        # non-blocking, use hub.sleep
        try:
            while True:
                status = self.poll()
                if status is not None:
                    return status
                hub.sleep(check_interval)

        except OSError, e:
            if e.errno == errno.ECHILD:
                # no child process, this happens if the child process
                # already died and has been cleaned up
                return -1
예제 #22
0
 def _set_size(self, size):
     if size < 0:
         raise ValueError('Size of the pool cannot be negative: %r' % (size, ))
     if size > self._maxsize:
         raise ValueError('Size of the pool cannot be bigger than maxsize: %r > %r' % (size, self._maxsize))
     if self.manager:
         self.manager.kill()
     while self._size < size:
         self._add_thread()
     delay = 0.0001
     while self._size > size:
         while self._size - size > self.task_queue.unfinished_tasks:
             self.task_queue.put(None)
         sleep(delay)
         delay = min(delay * 2, .05)
예제 #23
0
파일: monkey.py 프로젝트: lxsli/gevent
        def join(timeout=None):
            end = None
            if threading_mod.current_thread() is thread:
                raise RuntimeError("Cannot join current thread")
            if thread_greenlet is not None and thread_greenlet.dead:
                return
            if not thread.is_alive():
                return

            if timeout:
                end = time() + timeout

            while thread.is_alive():
                if end is not None and time() > end:
                    return
                sleep(0.01)
예제 #24
0
        def join(timeout=None):
            end = None
            if threading_mod.current_thread() is thread:
                raise RuntimeError("Cannot join current thread")
            if thread_greenlet is not None and thread_greenlet.dead:
                return
            if not thread.is_alive():
                return

            if timeout:
                end = time() + timeout

            while thread.is_alive():
                if end is not None and time() > end:
                    return
                sleep(0.01)
예제 #25
0
 def _set_size(self, size):
     if size < 0:
         raise ValueError('Size of the pool cannot be negative: %r' % (size, ))
     if size > self._maxsize:
         raise ValueError('Size of the pool cannot be bigger than maxsize: %r > %r' % (size, self._maxsize))
     if self.manager:
         self.manager.kill()
     while self._size < size:
         self._add_thread()
     delay = 0.0001
     while self._size > size:
         while self._size - size > self.task_queue.unfinished_tasks:
             self.task_queue.put(None)
         if getcurrent() is self.hub:
             break
         sleep(delay)
         delay = min(delay * 2, .05)
     if self._size:
         self.fork_watcher.start(self._on_fork)
     else:
         self.fork_watcher.stop()
예제 #26
0
 def _set_size(self, size):
     if size < 0:
         raise ValueError('Size of the pool cannot be negative: %r' % (size, ))
     if size > self._maxsize:
         raise ValueError('Size of the pool cannot be bigger than maxsize: %r > %r' % (size, self._maxsize))
     if self.manager:
         self.manager.kill()
     while self._size < size:
         self._add_thread()
     delay = 0.0001
     while self._size > size:
         while self._size - size > self.task_queue.unfinished_tasks:
             self.task_queue.put(None)
         if getcurrent() is self.hub:
             break
         sleep(delay)
         delay = min(delay * 2, .05)
     if self._size:
         self.fork_watcher.start(self._on_fork)
     else:
         self.fork_watcher.stop()
예제 #27
0
            def _shutdown():
                # Release anyone trying to join() me,
                # and let us switch to them.
                if not main_thread._tstate_lock:
                    return

                main_thread._tstate_lock.release()
                from gevent import sleep
                sleep()
                # Now, this may have resulted in us getting stopped
                # if some other greenlet actually just ran there.
                # That's not good, we're not supposed to be stopped
                # when we enter _shutdown.
                main_thread._is_stopped = False
                main_thread._tstate_lock = main_thread.__real_tstate_lock
                main_thread.__real_tstate_lock = None
                # The only truly blocking native shutdown lock to
                # acquire should be our own (hopefully), and the call to
                # _stop that orig_shutdown makes will discard it.

                orig_shutdown()
                patch_item(threading_mod, '_shutdown', orig_shutdown)
예제 #28
0
    def use_cloud(self,token):
        while True:
            if self.wav_queue.qsize():
                filename = self.readQ(queue=self.wav_queue)
            else:
                continue
            fp = wave.open(filename, 'rb')
            nf = fp.getnframes()
#             print 'sampwidth:',fp.getsampwidth()
#             print 'framerate:',fp.getframerate()
#             print 'channels:',fp.getnchannels()
            f_len = nf*2
            audio_data = fp.readframes(nf)
            
            cuid = 'xxxxxxxxxx'
            srv_url = 'http://vop.baidu.com/server_api'+'?cuid='+cuid+'&token='+token
            http_header = [
                'Content-Type:audio/pcm;rate=8000',
                'Content-length:%d' % f_len
            ]
            
            
            c = pycurl.Curl()
            c.setopt(pycurl.URL,str(srv_url)) #curl doesn't support unicode
            #c.setopt(c.RETURNTRANSE,1)
            c.setopt(c.HTTPHEADER,http_header) 
            c.setopt(c.POST,1)
            c.setopt(c.CONNECTTIMEOUT,80)
            c.setopt(c.TIMEOUT,80)
            c.setopt(c.WRITEFUNCTION,self.dump_res)
            c.setopt(c.POSTFIELDS,audio_data)
            c.setopt(c.POSTFIELDSIZE,f_len)
            try:
                c.perform() #pycurl.perform() has no return val
            except Exception as e:
                print e
            sleep(0.3)
예제 #29
0
    def test_feed_manager(self):
        """Test attack feed manager"""

        # Create a session manager and the feed manager attached to it
        config = Configuration()
        session_manager = SessionManager(config)
        feed_manager = FeedManager(config, session_manager)
        feed_manager.add_feed(DummyFeed(config))
        feed_manager.run()

        # Create an event
        event = Event("Test event")

        # Obtain a session and add the event
        session = session_manager.get_session("test", "127.0.0.1", 3200, "127.0.0.1", 3201)
        session.add_event(event)

        # Give the feed manager time for processing the event
        sleep(1)

        # Stop the feed manager and check if the event was processed
        feed_manager.stop()
        new_event = DummyFeed.events.get()
        self.assertIs(event, new_event)
예제 #30
0
def join(greenlet, polling_period=0.2):
    """Wait for a greenlet to finish by polling its status"""
    delay = 0.002
    while not greenlet.dead:
        delay = min(polling_period, delay * 2)
        sleep(delay)
예제 #31
0
 def _heartbeat(self, time):
     while self.socket:
         sleep(time)
         message = msg_base.ProtobufMessage(steammessages_clientserver_pb2.CMsgClientHeartBeat, EMsg.ClientHeartBeat)
         self.send_message(message)
예제 #32
0
 def join(self):
     delay = 0.0005
     while self.task_queue.unfinished_tasks > 0:
         sleep(delay)
         delay = min(delay * 2, .05)
예제 #33
0
 def join(self):
     """Waits until all outstanding tasks have been completed."""
     delay = 0.0005
     while self.task_queue.unfinished_tasks > 0:
         sleep(delay)
         delay = min(delay * 2, .05)
예제 #34
0
 def setUp(self):
     self.driver = webutils()
     self.driver.max_window()
     sleep(5)  # 隐性等待时间为30秒
     self.base_url = "https://www.baidu.com"
예제 #35
0
 def join(self):
     """Waits until all outstanding tasks have been completed."""
     delay = 0.0005
     while self.task_queue.unfinished_tasks > 0:
         sleep(delay)
         delay = min(delay * 2, .05)
예제 #36
0
파일: threadpool.py 프로젝트: gevent/gevent
 def join(self):
     """Waits until all outstanding tasks have been completed."""
     delay = max(0.0005, self.hub.loop.approx_timer_resolution)
     while self.task_queue.unfinished_tasks > 0:
         sleep(delay)
         delay = min(delay * 2, .05)
예제 #37
0
파일: threadpool.py 프로젝트: lznt/naali
 def kill(self):
     delay = 0.0005
     while self._size > 0:
         self._remove_threads(0)
         sleep(delay)
         delay = min(delay * 2, .05)
예제 #38
0
def join(greenlet, polling_period=0.2):
    """Wait for a greenlet to finish by polling its status"""
    delay = 0.002
    while not greenlet.dead:
        delay = min(polling_period, delay * 2)
        sleep(delay)
 def _heartbeat(self, time):
     while self.socket:
         sleep(time)
         message = msg_base.ProtobufMessage(steammessages_clientserver_pb2.CMsgClientHeartBeat, EMsg.ClientHeartBeat)
         self.send_message(message)
예제 #40
0
 def join(self):
     delay = 0.0005
     while self.task_queue.unfinished_tasks > 0:
         sleep(delay)
         delay = min(delay * 2, .05)
예제 #41
0
 def join(self):
     """Waits until all outstanding tasks have been completed."""
     delay = max(0.0005, self.hub.loop.approx_timer_resolution)
     while self.task_queue.unfinished_tasks > 0:
         sleep(delay)
         delay = min(delay * 2, .05)