本文整理汇总了Python中tornado.locks.Event.set方法的典型用法代码示例。如果您正苦于以下问题:Python Event.set方法的具体用法?Python Event.set怎么用?Python Event.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.locks.Event
的用法示例。
在下文中一共展示了Event.set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ResolveTimeoutTestCase
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
class ResolveTimeoutTestCase(AsyncHTTPTestCase):
def setUp(self):
self.cleanup_event = Event()
test = self
# Dummy Resolver subclass that never finishes.
class BadResolver(Resolver):
@gen.coroutine
def resolve(self, *args, **kwargs):
yield test.cleanup_event.wait()
# Return something valid so the test doesn't raise during cleanup.
return [(socket.AF_INET, ("127.0.0.1", test.get_http_port()))]
super(ResolveTimeoutTestCase, self).setUp()
self.http_client = SimpleAsyncHTTPClient(resolver=BadResolver())
def get_app(self):
return Application([url("/hello", HelloWorldHandler)])
def test_resolve_timeout(self):
with self.assertRaises(HTTPTimeoutError):
self.fetch("/hello", connect_timeout=0.1, raise_error=True)
# Let the hanging coroutine clean up after itself
self.cleanup_event.set()
self.io_loop.run_sync(lambda: gen.sleep(0))
示例2: __init__
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
class QueueDriver:
def __init__(self,**settings):
self.settings = settings
self._finished = Event()
self._getters = collections.deque([]) # Futures.
self._putters = collections.deque([])
self.initialize(**settings)
def initialize(self,**settings):
pass
def over(self):
self._finished.set()
def save(self):
raise NotImplementedError()
def get(self):
raise NotImplementedError()
def put(self):
raise NotImplementedError()
def join(self,timeout=None):
return self._finished.wait(timeout)
示例3: test_connect_timeout
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
def test_connect_timeout(self):
timeout = 0.1
cleanup_event = Event()
test = self
class TimeoutResolver(Resolver):
async def resolve(self, *args, **kwargs):
await cleanup_event.wait()
# Return something valid so the test doesn't raise during shutdown.
return [(socket.AF_INET, ("127.0.0.1", test.get_http_port()))]
with closing(self.create_client(resolver=TimeoutResolver())) as client:
with self.assertRaises(HTTPTimeoutError):
yield client.fetch(
self.get_url("/hello"),
connect_timeout=timeout,
request_timeout=3600,
raise_error=True,
)
# Let the hanging coroutine clean up after itself. We need to
# wait more than a single IOLoop iteration for the SSL case,
# which logs errors on unexpected EOF.
cleanup_event.set()
yield gen.sleep(0.2)
示例4: GameHandler
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
class GameHandler(WebSocketHandler):
def __init__(self, *args, **kwargs):
super(GameHandler, self).__init__(*args, **kwargs)
self.game = None
self.player = None
self.answered = Event()
def open(self):
pass
def auth(self, data):
self.game = GameLoader.load(data["game"])
self.player = data["player"]
self.game.handlers[self.player] = self
self.game.introduce(self.player)
def on_message(self, message):
data = Dumper.load(message)
if data["action"] == "auth":
self.auth(data)
if self.game is None:
self.close()
if data["action"] == "move":
self.game.take_action(self.player, data)
if data["action"] == "answer":
self.answered.set()
def on_close(self):
print("WebSocket closed")
def check_origin(self, origin):
return True
示例5: Waiter
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
class Waiter(object):
def __init__(self):
self.event = Event()
@gen.coroutine
def set(self):
self.event.set()
@gen.coroutine
def wait(self):
yield self.event.wait()
示例6: ImageMutex
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
class ImageMutex():
def __init__(self):
self._mutex = Event()
self._blocked = count()
self._building_log = []
self._exception = None
@gen.coroutine
def block(self):
value = self._blocked.__next__() # single bytecode operation
if value:
yield self._mutex.wait()
return value
def __enter__(self):
if self._exception is not None:
raise self._exception
return self
def __exit__(self, exc_type, exc_value, traceback):
self._building_log = []
if isinstance(exc_value, Exception):
self._exception = exc_value
self._mutex.set()
def timeout_happened(self):
self._exception = Exception('This image is too heavy to build')
self._building_log = []
def add_to_log(self, message, level=1):
if not self._exception:
self._building_log.append({
'text': message,
'level': level
})
@property
def building_log(self):
return self._building_log
@property
def last_exception(self):
return self._exception
示例7: _perform_heartbeat_loop
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
def _perform_heartbeat_loop(self):
if self._heartbeat_call is not None:
# TODO: cancel call
pass
cancellation_event = Event()
state_payload = self._subscription_state.state_payload()
presence_channels = self._subscription_state.prepare_channel_list(False)
presence_groups = self._subscription_state.prepare_channel_group_list(False)
if len(presence_channels) == 0 and len(presence_groups) == 0:
return
try:
envelope = yield self._pubnub.heartbeat() \
.channels(presence_channels) \
.channel_groups(presence_groups) \
.state(state_payload) \
.cancellation_event(cancellation_event) \
.future()
heartbeat_verbosity = self._pubnub.config.heartbeat_notification_options
if envelope.status.is_error:
if heartbeat_verbosity == PNHeartbeatNotificationOptions.ALL or \
heartbeat_verbosity == PNHeartbeatNotificationOptions.ALL:
self._listener_manager.announce_status(envelope.status)
else:
if heartbeat_verbosity == PNHeartbeatNotificationOptions.ALL:
self._listener_manager.announce_status(envelope.status)
except PubNubTornadoException:
pass
# TODO: check correctness
# if e.status is not None and e.status.category == PNStatusCategory.PNTimeoutCategory:
# self._start_subscribe_loop()
# else:
# self._listener_manager.announce_status(e.status)
except Exception as e:
print(e)
finally:
cancellation_event.set()
示例8: RedisStore
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
class RedisStore(BaseStore):
"""Publish data via a Redis backend.
This data store works in a similar manner as
:class:`DataStore`. The primary advantage is that external
programs can be used to publish data to be consumed by clients.
The ``channel`` keyword argument specifies which Redis channel to
publish to and defaults to ``tornadose``.
All remaining keyword arguments are passed directly to the
``redis.StrictRedis`` constructor. See `redis-py`__'s
documentation for detais.
New messages are read in a background thread via a
:class:`concurrent.futures.ThreadPoolExecutor`. This requires
either Python >= 3.2 or the backported ``futures`` module to be
installed.
__ https://redis-py.readthedocs.org/en/latest/
:raises ConnectionError: when the Redis host is not pingable
"""
def initialize(self, channel='tornadose', **kwargs):
if redis is None:
raise RuntimeError("The redis module is required to use RedisStore")
self.executor = ThreadPoolExecutor(max_workers=1)
self.channel = channel
self.messages = Queue()
self._done = Event()
self._redis = redis.StrictRedis(**kwargs)
self._redis.ping()
self._pubsub = self._redis.pubsub(ignore_subscribe_messages=True)
self._pubsub.subscribe(self.channel)
self.publish()
def submit(self, message, debug=False):
self._redis.publish(self.channel, message)
if debug:
logger.debug(message)
self._redis.setex(self.channel, 5, message)
def shutdown(self):
"""Stop the publishing loop."""
self._done.set()
self.executor.shutdown(wait=False)
@run_on_executor
def _get_message(self):
data = self._pubsub.get_message(timeout=1)
if data is not None:
data = data['data']
return data
@gen.coroutine
def publish(self):
while not self._done.is_set():
data = yield self._get_message()
if len(self.subscribers) > 0 and data is not None:
[subscriber.submit(data) for subscriber in self.subscribers]
示例9: Game
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
class Game(object):
roles = Enum('Role', [
'werewolf',
'seer',
'robber',
'troublemaker',
'villager',
])
ROLES_LABEL = {
roles.werewolf: ':wolf: Werewolf',
roles.seer: ':crystal_ball: Seer',
roles.robber: ':gun: Robber',
roles.troublemaker: ':smiling_imp: Troublemaker',
roles.villager: ':man: Villager',
}
GAME_STARTING = 'Starting game...'
CHECKING_PLAYERS = 'Checking players...'
INVALID_PLAYERS_LENGTH = 'You can only have 3-10 players in this channel ' \
'to start a game!'
GAME_STARTED = 'Everyone, pretend to close your eyes.'
CENTER_1 = ':black_joker: First card'
CENTER_2 = ':black_joker: Second card'
CENTER_3 = ':black_joker: Third card'
LOOK_OWN_CARD = ':black_joker: Everyone, look at your own card.'
LOOK_OWN_CARD_ACTION = 'Look'
LOOK_OWN_CARD_REVEAL = 'You are a {}'
WEREWOLF_WAKE_UP = ':wolf: Werewolves, wake up and look for other ' \
'werewolves.'
WEREWOLF_ATTACHMENT = 'If you are a werewolf...'
WEREWOLF_LOOK_FOR_OTHERS = 'Look for others'
WEREWOLF_LONE = 'You are the lone wolf'
WEREWOLF_LONE_LOOKED = 'You already looked at a center card'
WEREWOLF_NOT_LONE = 'You are not the lone wolf'
WEREWOLF_LIST = 'The other werewolves are: {}'
WEREWOLF_LONE_ATTACHMENT = 'If you are the lone wolf, check one of the ' \
'center cards...'
WEREWOLF_LOOK_AT_CENTER = 'The {} is a {}'
WEREWOLF_FALSE = 'You are not a werewolf!'
SEER_WAKE_UP = ':crystal_ball: Seer, wake up. You make look at another ' \
'player\'s card or two of the center cards.'
def __init__(self, bot_user_id, bot_access_token, channel_id, options):
self.id = str(uuid4())
self.bot_user_id = bot_user_id
self.bot_access_token = bot_access_token
self.channel_id = channel_id
self.redis = toredis.Client()
self.redis.connect(host=options.redis_host)
self.pubsub = toredis.Client()
self.pubsub.connect(host=options.redis_host)
self.pubsub.subscribe(self.id, callback=self.on_button)
def api(self, path, **kwargs):
data = kwargs.get('data', {})
data.setdefault('token', self.bot_access_token)
kwargs.update(data=data)
resp = requests.post(
'https://slack.com/api/{}'.format(path),
**kwargs).json()
if not resp['ok']:
raise RuntimeError(repr(resp['error']))
return resp
def start(self):
resp = self.api('rtm.start')
conn_future = websocket_connect(
resp['url'], on_message_callback=self.on_message)
ioloop = IOLoop.current()
ioloop.add_future(conn_future, self.on_connect)
def send(self, msg):
evt = {
'type': 'message',
'channel': self.channel_id,
'text': msg,
}
logging.info('Send: {}'.format(evt))
self.conn.write_message(json.dumps(evt))
def on_connect(self, conn_future):
self.conn = conn_future.result()
self.send(self.GAME_STARTING)
players = self.get_players()
if not (3 <= len(players) <= 5):
self.send(self.INVALID_PLAYERS_LENGTH)
return
roles = self.get_roles(players)
center = list(range(3))
self.player_roles = list(zip(players + center, roles))
logging.info(repr(self.player_roles))
ioloop = IOLoop.current()
ioloop.add_callback(self.start_night)
#.........这里部分代码省略.........
示例10: Queue
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
class Queue(object):
"""Coordinate producer and consumer coroutines.
If maxsize is 0 (the default) the queue size is unbounded.
.. testcode::
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.queues import Queue
q = Queue(maxsize=2)
@gen.coroutine
def consumer():
while True:
item = yield q.get()
try:
print('Doing work on %s' % item)
yield gen.sleep(0.01)
finally:
q.task_done()
@gen.coroutine
def producer():
for item in range(5):
yield q.put(item)
print('Put %s' % item)
@gen.coroutine
def main():
# Start consumer without waiting (since it never finishes).
IOLoop.current().spawn_callback(consumer)
yield producer() # Wait for producer to put all tasks.
yield q.join() # Wait for consumer to finish all tasks.
print('Done')
IOLoop.current().run_sync(main)
.. testoutput::
Put 0
Put 1
Doing work on 0
Put 2
Doing work on 1
Put 3
Doing work on 2
Put 4
Doing work on 3
Doing work on 4
Done
In Python 3.5, `Queue` implements the async iterator protocol, so
``consumer()`` could be rewritten as::
async def consumer():
async for item in q:
try:
print('Doing work on %s' % item)
yield gen.sleep(0.01)
finally:
q.task_done()
.. versionchanged:: 4.3
Added ``async for`` support in Python 3.5.
"""
def __init__(self, maxsize=0):
if maxsize is None:
raise TypeError("maxsize can't be None")
if maxsize < 0:
raise ValueError("maxsize can't be negative")
self._maxsize = maxsize
self._init()
self._getters = collections.deque([]) # Futures.
self._putters = collections.deque([]) # Pairs of (item, Future).
self._unfinished_tasks = 0
self._finished = Event()
self._finished.set()
@property
def maxsize(self):
"""Number of items allowed in the queue."""
return self._maxsize
def qsize(self):
"""Number of items in the queue."""
return len(self._queue)
def empty(self):
return not self._queue
def full(self):
if self.maxsize == 0:
return False
else:
return self.qsize() >= self.maxsize
#.........这里部分代码省略.........
示例11: WebSocketTest
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
#.........这里部分代码省略.........
def test_check_origin_valid_no_path(self):
port = self.get_http_port()
url = "ws://127.0.0.1:%d/echo" % port
headers = {"Origin": "http://127.0.0.1:%d" % port}
ws = yield websocket_connect(HTTPRequest(url, headers=headers))
ws.write_message("hello")
response = yield ws.read_message()
self.assertEqual(response, "hello")
@gen_test
def test_check_origin_valid_with_path(self):
port = self.get_http_port()
url = "ws://127.0.0.1:%d/echo" % port
headers = {"Origin": "http://127.0.0.1:%d/something" % port}
ws = yield websocket_connect(HTTPRequest(url, headers=headers))
ws.write_message("hello")
response = yield ws.read_message()
self.assertEqual(response, "hello")
@gen_test
def test_check_origin_invalid_partial_url(self):
port = self.get_http_port()
url = "ws://127.0.0.1:%d/echo" % port
headers = {"Origin": "127.0.0.1:%d" % port}
with self.assertRaises(HTTPError) as cm:
yield websocket_connect(HTTPRequest(url, headers=headers))
self.assertEqual(cm.exception.code, 403)
@gen_test
def test_check_origin_invalid(self):
port = self.get_http_port()
url = "ws://127.0.0.1:%d/echo" % port
# Host is 127.0.0.1, which should not be accessible from some other
# domain
headers = {"Origin": "http://somewhereelse.com"}
with self.assertRaises(HTTPError) as cm:
yield websocket_connect(HTTPRequest(url, headers=headers))
self.assertEqual(cm.exception.code, 403)
@gen_test
def test_check_origin_invalid_subdomains(self):
port = self.get_http_port()
url = "ws://localhost:%d/echo" % port
# Subdomains should be disallowed by default. If we could pass a
# resolver to websocket_connect we could test sibling domains as well.
headers = {"Origin": "http://subtenant.localhost"}
with self.assertRaises(HTTPError) as cm:
yield websocket_connect(HTTPRequest(url, headers=headers))
self.assertEqual(cm.exception.code, 403)
@gen_test
def test_subprotocols(self):
ws = yield self.ws_connect(
"/subprotocol", subprotocols=["badproto", "goodproto"]
)
self.assertEqual(ws.selected_subprotocol, "goodproto")
res = yield ws.read_message()
self.assertEqual(res, "subprotocol=goodproto")
@gen_test
def test_subprotocols_not_offered(self):
ws = yield self.ws_connect("/subprotocol")
self.assertIs(ws.selected_subprotocol, None)
res = yield ws.read_message()
self.assertEqual(res, "subprotocol=None")
@gen_test
def test_open_coroutine(self):
self.message_sent = Event()
ws = yield self.ws_connect("/open_coroutine")
yield ws.write_message("hello")
self.message_sent.set()
res = yield ws.read_message()
self.assertEqual(res, "ok")
@gen_test
def test_error_in_open(self):
with ExpectLog(app_log, "Uncaught exception"):
ws = yield self.ws_connect("/error_in_open")
res = yield ws.read_message()
self.assertIsNone(res)
@gen_test
def test_error_in_async_open(self):
with ExpectLog(app_log, "Uncaught exception"):
ws = yield self.ws_connect("/error_in_async_open")
res = yield ws.read_message()
self.assertIsNone(res)
示例12: __init__
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
class SlaveHolder:
def __init__(self, db, queue):
self.db = db
self.slaves = {}
self._finished = Event()
self._finished.set()
self.queue = queue
@coroutine
def start(self):
self._finished.clear()
logging.debug('Starting slave-holder')
cur = yield self.db.execute('SELECT * FROM registered_bots WHERE active = TRUE')
columns = [i[0] for i in cur.description]
while True:
row = cur.fetchone()
if not row:
break
row = dict(zip(columns, row))
self._start_bot(**row)
listen_future = self.queue.listen(slaveholder_queues(), self.queue_handler)
try:
yield self._finished.wait()
finally:
self.queue.stop(slaveholder_queues())
yield listen_future
def _start_bot(self, **kwargs):
@coroutine
def listen_done(f: Future):
logging.debug('[bot#%s] Terminated', kwargs['id'])
e = f.exception()
if e:
logging.debug('[bot#%s] Got exception: %s %s', kwargs['id'], format_exception(*f.exc_info()))
if isinstance(e, ApiError) and e.code == 401:
logging.warning('[bot#%d] Disabling due to connection error', kwargs['id'])
yield self.queue.send(QUEUE_BOTERATOR_BOT_REVOKE, dumps(dict(error=str(e), **kwargs)))
elif isinstance(e, ApiError) and e.code == 400 and 'chat not found' in e.description and \
str(kwargs['moderator_chat_id']) in e.request_body:
logging.warning('[bot#%d] Disabling due to unavailable moderator chat', kwargs['id'])
yield self.queue.send(QUEUE_BOTERATOR_BOT_REVOKE, dumps(dict(error=str(e), **kwargs)))
elif isinstance(e, ApiError) and e.code == 409 and 'webhook is active' in e.description:
logging.warning('[bot#%d] Disabling due to misconfigured webhook', kwargs['id'])
yield self.queue.send(QUEUE_BOTERATOR_BOT_REVOKE, dumps(dict(error=str(e), **kwargs)))
else:
IOLoop.current().add_timeout(timedelta(seconds=5), self._start_bot, **kwargs)
del self.slaves[kwargs['id']]
slave = Slave(db=self.db, **kwargs)
slave_listen_f = slave.start()
self.slaves[kwargs['id']] = {
'future': slave_listen_f,
'instance': slave,
}
IOLoop.current().add_future(slave_listen_f, listen_done)
def stop(self):
logging.info('Stopping slave-holder')
for slave in self.slaves.values():
slave['instance'].stop()
self._finished.set()
@coroutine
def queue_handler(self, queue_name, body):
body = loads(body.decode('utf-8'))
if queue_name == QUEUE_SLAVEHOLDER_NEW_BOT:
self._start_bot(**body)
elif queue_name == QUEUE_SLAVEHOLDER_GET_BOT_INFO:
bot = Api(body['token'], lambda x: None)
if bot.bot_id in self.slaves:
logging.debug('[bot#%s] Already registered', bot.bot_id)
yield self.queue.send(body['reply_to'], dumps(dict(error='duplicate')))
try:
ret = yield bot.get_me()
logging.debug('[bot#%s] Ok', bot.bot_id)
except Exception as e:
logging.debug('[bot#%s] Failed', bot.bot_id)
yield self.queue.send(body['reply_to'], dumps(dict(error=str(e))))
return
yield self.queue.send(body['reply_to'], dumps(ret))
elif queue_name == QUEUE_SLAVEHOLDER_GET_MODERATION_GROUP:
update_with_command_f = Future()
timeout_f = with_timeout(timedelta(seconds=body['timeout']), update_with_command_f)
@coroutine
def slave_update_handler(update):
logging.debug('[bot#%s] Received update', bot.bot_id)
if attach_cmd_filter.test(**update):
logging.debug('[bot#%s] /attach', bot.bot_id)
#.........这里部分代码省略.........
示例13: WebSocketTest
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
#.........这里部分代码省略.........
def test_coroutine(self):
ws = yield self.ws_connect('/coroutine')
# Send both messages immediately, coroutine must process one at a time.
yield ws.write_message('hello1')
yield ws.write_message('hello2')
res = yield ws.read_message()
self.assertEqual(res, 'hello1')
res = yield ws.read_message()
self.assertEqual(res, 'hello2')
@gen_test
def test_check_origin_valid_no_path(self):
port = self.get_http_port()
url = 'ws://127.0.0.1:%d/echo' % port
headers = {'Origin': 'http://127.0.0.1:%d' % port}
ws = yield websocket_connect(HTTPRequest(url, headers=headers))
ws.write_message('hello')
response = yield ws.read_message()
self.assertEqual(response, 'hello')
yield self.close(ws)
@gen_test
def test_check_origin_valid_with_path(self):
port = self.get_http_port()
url = 'ws://127.0.0.1:%d/echo' % port
headers = {'Origin': 'http://127.0.0.1:%d/something' % port}
ws = yield websocket_connect(HTTPRequest(url, headers=headers))
ws.write_message('hello')
response = yield ws.read_message()
self.assertEqual(response, 'hello')
yield self.close(ws)
@gen_test
def test_check_origin_invalid_partial_url(self):
port = self.get_http_port()
url = 'ws://127.0.0.1:%d/echo' % port
headers = {'Origin': '127.0.0.1:%d' % port}
with self.assertRaises(HTTPError) as cm:
yield websocket_connect(HTTPRequest(url, headers=headers))
self.assertEqual(cm.exception.code, 403)
@gen_test
def test_check_origin_invalid(self):
port = self.get_http_port()
url = 'ws://127.0.0.1:%d/echo' % port
# Host is 127.0.0.1, which should not be accessible from some other
# domain
headers = {'Origin': 'http://somewhereelse.com'}
with self.assertRaises(HTTPError) as cm:
yield websocket_connect(HTTPRequest(url, headers=headers))
self.assertEqual(cm.exception.code, 403)
@gen_test
def test_check_origin_invalid_subdomains(self):
port = self.get_http_port()
url = 'ws://localhost:%d/echo' % port
# Subdomains should be disallowed by default. If we could pass a
# resolver to websocket_connect we could test sibling domains as well.
headers = {'Origin': 'http://subtenant.localhost'}
with self.assertRaises(HTTPError) as cm:
yield websocket_connect(HTTPRequest(url, headers=headers))
self.assertEqual(cm.exception.code, 403)
@gen_test
def test_subprotocols(self):
ws = yield self.ws_connect('/subprotocol', subprotocols=['badproto', 'goodproto'])
self.assertEqual(ws.selected_subprotocol, 'goodproto')
res = yield ws.read_message()
self.assertEqual(res, 'subprotocol=goodproto')
yield self.close(ws)
@gen_test
def test_subprotocols_not_offered(self):
ws = yield self.ws_connect('/subprotocol')
self.assertIs(ws.selected_subprotocol, None)
res = yield ws.read_message()
self.assertEqual(res, 'subprotocol=None')
yield self.close(ws)
@gen_test
def test_open_coroutine(self):
self.message_sent = Event()
ws = yield self.ws_connect('/open_coroutine')
yield ws.write_message('hello')
self.message_sent.set()
res = yield ws.read_message()
self.assertEqual(res, 'ok')
yield self.close(ws)
示例14: RDKafkaDrain
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
class RDKafkaDrain(object):
"""Implementation of IDrain that produces to a Kafka topic using librdkafka
asynchronously. Backpressure is implemented with a tornado.queues.Queue.
Expects an instance of confluent_kafka.Producer as self.sender.
"""
def __init__(self, logger, loop, producer, topic, **kwargs):
self.emitter = producer
self.logger = logger
self.loop = loop
self.loop.spawn_callback(self._poll)
self._completed = Queue()
self._ignored_errors = set(kwargs.get('ignored_errors', []))
# See: https://github.com/confluentinc/confluent-kafka-python/issues/147
self._ignored_errors.update(TRANSIENT_ERRORS)
self.metric_prefix = kwargs.get('metric_prefix', 'emitter')
self.output_error = Event()
self.sender_tag = 'sender:%s.%s' % (self.__class__.__module__,
self.__class__.__name__)
self.topic = topic
self.state = RUNNING
@gen.coroutine
def close(self, timeout=INITIAL_TIMEOUT):
try:
self.state = CLOSING
begin = datetime.utcnow()
num_messages = len(self.emitter)
elapsed = datetime.utcnow() - begin
while num_messages > 0 and elapsed <= MAX_TIMEOUT:
self.logger.info("Flushing send queue in %s/%s: %d",
elapsed, MAX_TIMEOUT, num_messages)
self.emitter.poll(0)
num_messages = len(self.emitter)
elapsed = datetime.utcnow() - begin
timeout = min(timeout*2, MAX_TIMEOUT)
yield gen.sleep(timeout.total_seconds())
else:
self.logger.error('Unable to flush messages; aborting')
finally:
self.state = CLOSED
def emit_nowait(self, msg):
self.logger.debug("Drain emitting")
try:
if self.topic == 'dynamic':
assert isinstance(msg, KafkaMessage)
self.emitter.produce(
msg.topic,
msg.value,
msg.key,
# This callback is executed in the librdkafka thread
callback=self._trampoline,
)
else:
assert isinstance(msg, basestring)
self.emitter.produce(
self.topic, msg,
# This callback is executed in the librdkafka thread
callback=self._trampoline,
)
except BufferError:
raise QueueFull()
@gen.coroutine
def emit(self, msg, retry_timeout=INITIAL_TIMEOUT):
while True:
try:
self.emit_nowait(msg)
except QueueFull:
yield gen.sleep(retry_timeout.total_seconds())
retry_timeout = min(retry_timeout*2, MAX_TIMEOUT)
@gen.coroutine
def _poll(self, retry_timeout=INITIAL_TIMEOUT):
"""Infinite coroutine for draining the delivery report queue,
with exponential backoff.
"""
try:
num_processed = self.emitter.poll(0)
if num_processed > 0:
self.logger.debug("Drain received ack for messages: %d",
num_processed)
retry_timeout = INITIAL_TIMEOUT
else:
self.logger.debug("Drain delivery report queue empty")
# Retry with exponential backoff
yield gen.sleep(retry_timeout.total_seconds())
retry_timeout = min(retry_timeout*2, MAX_TIMEOUT)
finally:
self.loop.spawn_callback(self._poll, retry_timeout)
@gen.coroutine
def _on_track(self, err, kafka_msg):
self.logger.debug('Received delivery notification: "%s", "%s"',
err, kafka_msg)
if err:
if err.code() in self._ignored_errors:
self.logger.warning('Ignoring error: %s', err)
else:
#.........这里部分代码省略.........
示例15: ProjectGroomer
# 需要导入模块: from tornado.locks import Event [as 别名]
# 或者: from tornado.locks.Event import set [as 别名]
class ProjectGroomer(object):
""" Cleans up expired transactions for a project. """
def __init__(self, project_id, coordinator, zk_client, db_access,
thread_pool):
""" Creates a new ProjectGroomer.
Args:
project_id: A string specifying a project ID.
coordinator: A GroomingCoordinator.
zk_client: A KazooClient.
db_access: A DatastoreProxy.
thread_pool: A ThreadPoolExecutor.
"""
self.project_id = project_id
self._coordinator = coordinator
self._zk_client = zk_client
self._tornado_zk = TornadoKazoo(self._zk_client)
self._db_access = db_access
self._thread_pool = thread_pool
self._project_node = '/appscale/apps/{}'.format(self.project_id)
self._containers = []
self._inactive_containers = set()
self._batch_resolver = BatchResolver(self.project_id, self._db_access)
self._zk_client.ensure_path(self._project_node)
self._zk_client.ChildrenWatch(self._project_node, self._update_containers)
self._txid_manual_offset = 0
self._offset_node = '/'.join([self._project_node, OFFSET_NODE])
self._zk_client.DataWatch(self._offset_node, self._update_offset)
self._stop_event = AsyncEvent()
self._stopped_event = AsyncEvent()
# Keeps track of cleanup results for each round of grooming.
self._txids_cleaned = 0
self._oldest_valid_tx_time = None
self._worker_queue = AsyncQueue(maxsize=MAX_CONCURRENCY)
for _ in range(MAX_CONCURRENCY):
IOLoop.current().spawn_callback(self._worker)
IOLoop.current().spawn_callback(self.start)
@gen.coroutine
def start(self):
""" Starts the grooming process until the stop event is set. """
logger.info('Grooming {}'.format(self.project_id))
while True:
if self._stop_event.is_set():
break
try:
yield self._groom_project()
except Exception:
# Prevent the grooming loop from stopping if an error is encountered.
logger.exception(
'Unexpected error while grooming {}'.format(self.project_id))
yield gen.sleep(MAX_TX_DURATION)
self._stopped_event.set()
@gen.coroutine
def stop(self):
""" Stops the grooming process. """
logger.info('Stopping grooming process for {}'.format(self.project_id))
self._stop_event.set()
yield self._stopped_event.wait()
@gen.coroutine
def _worker(self):
""" Processes items in the worker queue. """
while True:
tx_path, composite_indexes = yield self._worker_queue.get()
try:
tx_time = yield self._resolve_txid(tx_path, composite_indexes)
if tx_time is None:
self._txids_cleaned += 1
if tx_time is not None and tx_time < self._oldest_valid_tx_time:
self._oldest_valid_tx_time = tx_time
finally:
self._worker_queue.task_done()
def _update_offset(self, new_offset, _):
""" Watches for updates to the manual offset node.
Args:
new_offset: A string specifying the new manual offset.
"""
self._txid_manual_offset = int(new_offset or 0)
def _update_containers(self, nodes):
""" Updates the list of active txid containers.
Args:
nodes: A list of strings specifying ZooKeeper nodes.
"""
counters = [int(node[len(CONTAINER_PREFIX):] or 1)
#.........这里部分代码省略.........