How to use the tornado.gen.Task function in tornado

To help you get started, we’ve selected a few tornado examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github holmes-app / holmes-api / tests / unit / test_cache.py View on Github external
domain_url = 'http://globo.com'

        key1 = 'limit-for-%s' % domain_url
        self.cache.redis.delete(key1)

        key2 = 'limit-for-%s/sa/' % domain_url
        self.cache.redis.delete(key2)

        yield Task(self.cache.redis.zadd, key1, {'a': 1})
        yield Task(self.cache.redis.zadd, key2, {'b': 1})

        keys = yield Task(self.cache.redis.keys, 'limit-for-%s*' % domain_url)
        expect(keys).to_length(2)

        yield Task(self.cache.delete_limit_usage_by_domain, domain_url)
        keys = yield Task(self.cache.redis.keys, 'limit-for-%s*' % domain_url)
        expect(keys).to_length(0)
github holmes-app / holmes-api / tests / unit / test_cache.py View on Github external
def test_can_delete_domain_violations_prefs(self):
        domain_url = 'globo.com'
        key = 'violations-prefs-%s' % domain_url

        self.cache.redis.delete(key)
        prefs = yield Task(self.cache.redis.get, key)
        expect(prefs).to_be_null()

        data = dumps([{'key': 'test', 'value': '10'}])
        yield Task(self.cache.redis.setex, key, 1, data)
        prefs = yield Task(self.cache.redis.get, key)
        expect(prefs).to_be_like(data)

        yield self.cache.delete_domain_violations_prefs(domain_url)

        prefs = yield Task(self.cache.redis.get, key)
        expect(prefs).to_be_null()
github moddevices / mod-ui / mod / control_chain.py View on Github external
def check_read_response(self, resp):
        try:
            data = json.loads(resp[:-1].decode("utf-8", errors="ignore"))
        except:
            print("ERROR: Control Chain read response failed")
        else:
            if 'event' not in data.keys():
                print("ERROR: Control Chain read response invalid, missing 'event' field", data)

            elif data['event'] == "device_status":
                data   = data['data']
                dev_id = data['device_id']

                if data['status']:
                    yield gen.Task(self.send_device_descriptor, dev_id)

                else:
                    try:
                        hw_data = self.hw_versions.pop(dev_id)
                    except KeyError:
                        print("ERROR: Control Chain device removed, but not on current list!?", dev_id)
                    else:
                        dev_uri, label, labelsuffix, version = hw_data
                        self.hw_removed_cb(dev_id, dev_uri, label+labelsuffix, version)

        finally:
            self.process_read_queue()
github marcelnicolay / mongotor / mongotor / client.py View on Github external
:Parameters:
          - `pipeline`: a single command or list of aggregation commands
          - `read_preference`

        .. note:: Requires server version **>= 2.1.0**

        .. _aggregate command:
            http://docs.mongodb.org/manual/applications/aggregation
        """
        if not isinstance(pipeline, (dict, list, tuple)):
            raise TypeError("pipeline must be a dict, list or tuple")

        if isinstance(pipeline, dict):
            pipeline = [pipeline]

        response, error = yield gen.Task(self._database.command, "aggregate",
                                         self._collection, pipeline=pipeline,
                                         read_preference=read_preference)

        callback(response)
github moddevices / mod-ui / mod / webserver.py View on Github external
def install_bundles_in_tmp_dir(callback):
    error     = ""
    removed   = []
    installed = []

    for bundle in os.listdir(DOWNLOAD_TMP_DIR):
        tmppath    = os.path.join(DOWNLOAD_TMP_DIR, bundle)
        bundlepath = os.path.join(LV2_PLUGIN_DIR, bundle)

        if os.path.exists(bundlepath):
            resp, data = yield gen.Task(SESSION.host.remove_bundle, bundlepath, True)

            # When removing bundles we can ignore the ones that are not loaded
            # It can happen if a previous install failed abruptely
            if not resp and data == "Bundle not loaded":
                resp = True
                data = []

            if resp:
                removed += data
                shutil.rmtree(bundlepath)
            else:
                error = data
                break

        shutil.move(tmppath, bundlepath)
        resp, data = yield gen.Task(SESSION.host.add_bundle, bundlepath)
github equeny / tornadomail / tornadomail / backends / smtp.py View on Github external
def send_messages(self, email_messages, callback=None):
        """
        Sends one or more EmailMessage objects and returns the number of email
        messages sent.
        """
        if not email_messages:
            return

        new_conn_created = yield gen.Task(self.open)
        if not self.connection:
            # We failed silently on open().
            # Trying to send would be pointless.
            return
        num_sent = 0
        for message in email_messages:
            sent = yield gen.Task(self._send, message)
            if sent:
                num_sent += 1
        if new_conn_created:
            yield gen.Task(self.close)
        if callback:
            callback(num_sent)
github kzahel / ktorrent / ktorrent / tracker.py View on Github external
def announce(self, callback=None):
        logging.info('%s announcing' % self)
        if not self.http_client:
            Tracker.http_client = httpclient.AsyncHTTPClient()

        if not self.can_announce():
            if callback:
                callback(None)

        self.last_announce = time.time()


        response = yield gen.Task( self.http_client.fetch, '%s?_tracker_url=%s&info_hash=%s&compact=1&callback=mycallback' % (options.tracker_proxy, urllib.quote(self.url), urllib.quote(self.infohash) ) )

        if response.code == 200:

            if self.proxy_mode:
                rawdata = response.body
                b64data = rawdata[ rawdata.find('(')+2 : rawdata.find(')', len(rawdata) - 1)-1 ]
                data = bencode.bdecode(base64.b64decode(b64data))

            else:
                data = bencode.bdecode(response.body)

            peerdata = []
            if 'peers' in data:
                for i in range(len(data['peers'])/6):
                    decoded = decode_peer( data['peers'][i*6:(i+1)*6] )
                    if decoded[1] != 0:
github peterbe / tiler / server / handlers.py View on Github external
}
            if (page - 1) * page_size > 0:
                pagination['prev'] = page - 1
            if page * page_size < total_count:
                pagination['next'] = page + 1

        data['pagination'] = pagination
        data['show_hero_unit'] = self.get_argument('page', None) is None
        data['total_count'] = total_count
        if total_count:
            _cache_key = 'totalstats'
            value = self.redis.get(_cache_key)
            if value:
                stats = tornado.escape.json_decode(value)
            else:
                fileids = yield tornado.gen.Task(
                    self.get_all_fileids,
                    user=self.get_argument('user', None)
                )
                stats = self.get_stats_by_fileids(
                    fileids,
                    user=self.get_argument('user', None)
                )
                self.redis.setex(
                    _cache_key,
                    tornado.escape.json_encode(stats),
                    60
                )
            data['total_bytes_served'] = stats['total_bytes_served']
            data['total_hits'] = stats['total_hits']
            data['total_hits_this_month'] = stats['total_hits_this_month']
github UniPiTechnology / evok / evok / evok.py View on Github external
sub_process = tornado.process.Subprocess(
        cmd, stdin=stdin, stdout=Subprocess.STREAM, stderr=Subprocess.STREAM, shell=True
    )

    if stdin_data:
        if stdin_async:
            yield Subprocess.Task(sub_process.stdin.write, stdin_data)
        else:
            sub_process.stdin.write(stdin_data)

    if stdin_async or stdin_data:
        sub_process.stdin.close()

    result, error = yield [
        gen.Task(sub_process.stdout.read_until_close),
        gen.Task(sub_process.stderr.read_until_close)
    ]

    raise gen.Return((result, error))
github wickman / rainman / rainman / peer_driver.py View on Github external
def recv(self):
    """Receive a message on the iostream, and dispatch to the provided interface."""
    message_length = struct.unpack('>I', (yield gen.Task(self.iostream.read_bytes, 4)))[0]
    if message_length == 0:
      yield self.keepalive()
    else:
      message_body = yield gen.Task(self.iostream.read_bytes, message_length)
      message_id, message_body = ord(message_body[0]), message_body[1:]
      yield self._dispatch(self, message_id, message_body)