This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@gen.coroutine | |
def rpc(self, exchange, routing_key, body, timeout=None): | |
""" | |
rpc call. It create a queue randomly when encounters first call with the same exchange name. Then, it starts | |
consuming the created queue(waiting result). It publishes message to rabbitmq with properties that has correlation_id | |
and reply_to. if timeout is set, it starts a coroutine to wait timeout and raises an `Exception("timeout")`. | |
If server has been sent result, it return it asynchronously. | |
:param exchange: exchange name | |
:param routing_key: routing key(e.g. dog.Yellow, cat.big) | |
:param body: message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding:utf-8 -*- | |
from __future__ import unicode_literals | |
import unittest | |
import uuid | |
import time | |
from rabbitmq.rabbitmq_rpc import AsyncRabbitMQ | |
from rabbitmq.rabbitmq_util import make_properties | |
from tornado.gen import coroutine,Return, sleep | |
from tornado.testing import AsyncTestCase, gen_test | |
from tornado.queues import Queue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SyncRabbitMQProducer(object): | |
""" | |
synchronize amqp producer | |
usage: | |
with SyncAMQPProducer("127.0.0.1") as p: | |
p.publish("exchange_name", "dog.black", "message1", "message2") | |
""" | |
def __init__(self, rabbitmq_url): | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@gen.coroutine | |
def connect(self): | |
""" | |
establishing two connections for publishing and receiving respectively. | |
:return: True if establish successfully. | |
""" | |
self._publish_connection = yield self._create_connection(self._parameter) | |
self._receive_connection = yield self._create_connection(self._parameter) | |
raise gen.Return(True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _create_channel(self, connection): | |
self.logger.info("creating channel") | |
future = Future() | |
def on_channel_closed(channel, reply_code, reply_txt): | |
if reply_code not in [self._NORMAL_CLOSE_CODE, self._USER_CLOSE_CODE]: | |
self.logger.error("channel closed. reply code: %s; reply text: %s. system will exist" | |
% (reply_code, reply_txt,)) | |
sys.exit(self._EXIST_CODE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _exchange_declare(self, channel, exchange=None, exchange_type='topic', **kwargs): | |
self.logger.info("declaring exchange: %s " % exchange) | |
future = Future() | |
def callback(unframe): | |
self.logger.info("declared exchange: %s" % exchange) | |
future.set_result(unframe) | |
channel.exchange_declare(callback=callback, | |
exchange=exchange, exchange_type=exchange_type, **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@gen.coroutine | |
def publish(self, exchange, routing_key, body, properties=None): | |
""" | |
publish message. creating a brand new channel once invoke this method. After publishing, it closes the | |
channel. | |
:param exchange: exchange name | |
:type exchange; str or unicode | |
:param routing_key: routing key (e.g. dog.yellow, cat.big) | |
:param body: message | |
:param properties: properties |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@gen.coroutine | |
def receive(self, exchange, routing_key, queue_name, handler, no_ack=False, prefetch_count=0): | |
""" | |
receive message. creating a brand new channel to consume message. Before consuming, it have to declaring | |
exchange and queue. And bind queue to particular exchange with routing key. if received properties is not | |
none, it publishes result back to `reply_to` queue. | |
:param exchange: exchange name | |
:param routing_key: routing key (e.g. dog.*, *.big) | |
:param queue_name: queue name | |
:param handler: message handler, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// program.cs | |
static void Main(string[] args) | |
{ | |
const string WELCOME = "Welcome to Calculator. Feel free to type any expression you want."; | |
const string PROMPT = ">> "; | |
Console.Out.Write(WELCOME + Environment.NewLine); | |
while(true) | |
{ | |
Console.Out.Write(PROMPT); | |
try |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Token.cs | |
public enum TokenType | |
{ | |
INT, | |
PLUS, | |
MINUS, | |
MULTIPLY, | |
DIVIDE, | |
POWER, | |
LPAREN, |
OlderNewer