def while_session_get(page_url, times=5000, sleep_time=0.2, new_sess=False): """ 循环请求 :return: """ while_times = 0 while True: try: if new_sess: global sess sess = requests.session() gevent_sleep(random.randint(5, 8) * sleep_time) result = sess.get(page_url, headers=get_header(), timeout=5) # result = requests.get(page_url, headers=headers, proxies=proxies, timeout=5) except Exception as e: if while_times < times: while_times += 1 print('**********', '尝试重新链接', while_times, '次:', page_url.replace(home_url, '')) continue else: raise e else: return result
def _sleep(mode: FeatureMode) -> None: if mode is FeatureMode.Parallel or mode is FeatureMode.Concurrent: time.sleep(_Sleep_Time) elif mode is FeatureMode.GreenThread: gevent_sleep(_Sleep_Time) elif mode is FeatureMode.Asynchronous: asyncio.sleep(_Sleep_Time) else: raise ValueError
def _target_testing(): # Save a time stamp into list try: _thread_id = get_gevent_ident() gevent_sleep(_Sleep_Time) _time = float(time.time()) _done_timestamp[_thread_id] = _time except Exception as e: assert False, f"Occur something unexpected issue. Please check it. Exception: {e}" else: assert True, "Testing code successfully."
def userset_greenlet(): """ Greenlet that polls redis for changes in the user sets of each room. This is done by calling the userset update function every few seconds. """ while True: gevent_sleep(5) try: local_red = StrictRedis.from_url(app.config.get("REDIS_URL")) userset_update(local_red) except: app.logger.error("Exception in user set polling greenlet.", exc_info=True)
def advance(self, amount=0): """Move time on this clock forward by the given amount and run whatever pending calls should be run. """ # First we yield the control so that other greenlets have a # chance to run. gevent_sleep() future = self.right_now + amount while self.calls and self.calls[0].time <= future: dc = self.calls.pop(0) self.right_now = dc.time dc() gevent_sleep() self.right_now = max(future, self.right_now)
def room_update_loop(): """Greenlet that calls check_video_ended on all the rooms every few seconds.""" while True: gevent_sleep(2) try: dbsession = db.Session(db.engine) # Find all rooms whose videos are playing. rooms = dbsession.query(Room).filter_by(is_playing=True).all() for room in rooms: try: room.check_video_ended() except: app.logger.error("Exception updating room %s." % room.slug, exc_info=True) gevent_sleep(0) dbsession.close() except: app.logger.error("Exception in room update loop.", exc_info=True)
def sleep(seconds: int, ref: bool = True) -> None: gevent_sleep(seconds=seconds, ref=ref)
def echo_sleep(t): print "%r sleep %s" % (echo.bound, t) gevent_sleep(t) print "end of sleep" return t
def _target_testing(): # Save a timestamp into list _thread_id = get_gevent_ident() gevent_sleep(_Sleep_Time) _time = float(time.time()) _done_timestamp[_thread_id] = _time
def sleep(self, seconds=0): """Sleep for C{seconds}.""" gevent_sleep(seconds)
def bar(): print('Explicit context to bar') gevent_sleep(0) print('Implicit context switch back to bar')
def foo(): print('Running in foo') gevent_sleep(0) print('Explicit context switch to foo again')
def async_sleep(s, msg=''): if s>0: SLEEP_DEBUG("[async sleep] %s secs: %s" % (s, msg)) gevent_sleep(s)