Beispiel #1
0
 def __init__(self, redis_pool, message_handler, pubsub_key):
     self.pubsub_key = pubsub_key
     self.message_handler = message_handler
     self.redis_pool = redis_pool
     self.subscriber = None
     self.message_loop_task = None
     asyncio.new_event_loop().run_until_complete(self.setup_subscriber())
Beispiel #2
0
def test_loop_asyncio():
    import asyncio

    aio_loop = asyncio.new_event_loop()
    loop.integrate(aio_loop, reset=False)

    res = []
    def callback():
        res.append(1)

    loop.call_soon(callback)
    aio_loop.stop()
    aio_loop.run_forever()

    assert len(res) == 1

    # Now run wrong loop
    aio_loop = asyncio.new_event_loop()
    # loop.integrate(aio_loop, reset=False)  -> dont do this (yet)

    loop.call_soon(callback)
    aio_loop.stop()
    aio_loop.run_forever()

    assert len(res) == 1

    loop.integrate(aio_loop, reset=False)  # but do it now
    aio_loop.stop()
    aio_loop.run_forever()

    aio_loop.stop()
    aio_loop.run_forever()

    assert len(res) == 2
Beispiel #3
0
def test_flexx_in_thread1():
    """ Test threading and ioloop selection.
    """

    def main():
        asyncio.set_event_loop(loop2)
        app.create_server()

    # Create 3 loops, nr 2 is made current in the thread
    loop1 = asyncio.new_event_loop()
    loop2 = asyncio.new_event_loop()
    loop3 = asyncio.new_event_loop()

    asyncio.set_event_loop(loop1)
    server1 = app.create_server()

    t = threading.Thread(target=main)
    t.start()
    t.join()
    server2 = app.current_server()  # still current as set by the thread

    asyncio.set_event_loop(loop3)
    server3 = app.create_server()

    assert server1._loop is loop1
    assert server2._loop is loop2
    assert server3._loop is loop3
Beispiel #4
0
def test_rebinding_ioloop():
    """ Test recreating server objects, and its binding to the current ioloop.
    """

    res = []
    def add_res(i):
        res.append(i)

    # Create new ioloop
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    # Create new flexx server, which binds to that loop
    server1 = app.create_server()
    assert server1 is app.current_server()
    #
    assert loop is server1._loop

    # Create new ioloop
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    # This is a new loop
    assert loop is not server1._loop

    # Create new flexx server, which binds to that loop
    server2 = app.create_server()
    assert server2 is app.current_server()
    assert server1 is not server2
    #
    assert loop is server2._loop
Beispiel #5
0
 def get_loop(self):
     if self.loop is None:
         try:
             self.loop = asyncio.get_event_loop()
         except RuntimeError:
             # attempt to recover by making new loop
             self.loop = asyncio.new_event_loop()
             asyncio.set_event_loop(self.loop)
     if self.loop.is_closed():
         self.loop = asyncio.new_event_loop()
         asyncio.set_event_loop(self.loop)
     return self.loop
    def test_asyncio_composition_checks_for_same_loop(self):
        import asyncio

        f1 = asyncio.Future(loop=asyncio.new_event_loop())
        f2 = asyncio.Future(loop=asyncio.new_event_loop())

        self.assertRaises(ValueError, asyncio.Future.gather, [f1, f2])
        self.assertRaises(ValueError, asyncio.Future.first, [f1, f2])
        self.assertRaises(ValueError, asyncio.Future.first_successful, [f1, f2])

        f1 = asyncio.Future.failed(TypeError())
        f3 = f1.fallback(f2)
        self.assertRaises(ValueError, asyncio.get_event_loop().run_until_complete, f3)
    def test_server_in_separate_thread(self):
        sock = socket.socket()
        server_loop = asyncio.new_event_loop()
        server_loop.set_debug(True)
        server, server_thread = self._start_server_thread(server_loop, sock)

        client_loop = asyncio.new_event_loop()
        client_loop.set_debug(True)
        add_result = client_loop.run_until_complete(
            test_server_with_client(sock, client_loop),
        )
        self.assertEqual(42, add_result)

        server_loop.call_soon_threadsafe(server.close)
        server_thread.join()
Beispiel #8
0
    def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(None)

        self.transport = unittest.mock.Mock()
        self.stream = aiohttp.StreamParser(loop=self.loop)
        self.response = HttpResponse('get', 'http://python.org')
Beispiel #9
0
def main():
    asyncio.set_event_loop(None)
    if args.iocp:
        from asyncio.windows_events import ProactorEventLoop
        loop = ProactorEventLoop()
    else:
        loop = asyncio.new_event_loop()
    sslctx = None
    if args.tls:
        import ssl
        # TODO: take cert/key from args as well.
        here = os.path.join(os.path.dirname(__file__), '..', 'tests')
        sslctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        sslctx.options |= ssl.OP_NO_SSLv2
        sslctx.load_cert_chain(
            certfile=os.path.join(here, 'ssl_cert.pem'),
            keyfile=os.path.join(here, 'ssl_key.pem'))
    cache = Cache(loop)
    task = asyncio.streams.start_server(cache.handle_client,
                                        args.host, args.port,
                                        ssl=sslctx, loop=loop)
    svr = loop.run_until_complete(task)
    for sock in svr.sockets:
        logging.info('socket %s', sock.getsockname())
    try:
        loop.run_forever()
    finally:
        loop.close()
Beispiel #10
0
 def test_ctor_with_default_loop(self):
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     self.addCleanup(loop.close)
     self.addCleanup(asyncio.set_event_loop, None)
     conn = aiohttp.BaseConnector()
     self.assertIs(loop, conn._loop)
Beispiel #11
0
    def seed_fetcher(self, fetcher_inbox):
        loop = new_event_loop()
        ActorCls = FetcherSeeder.init(self.seed_path)
        outbox = UnixSocketClient(fetcher_inbox, loop)
        actor = ActorCls(None, outbox, loop=loop)

        loop.run_until_complete(actor.start())
Beispiel #12
0
 def setUp(self):
     # This simulates the effect of an asyncio test harness like
     # pytest-asyncio.
     self.orig_loop = asyncio.get_event_loop()
     self.new_loop = asyncio.new_event_loop()
     asyncio.set_event_loop(self.new_loop)
     super(GetNewIOLoopTest, self).setUp()
Beispiel #13
0
 def add_task(self, task_id):
     try:
         loop = asyncio.get_event_loop()
     except RuntimeError:
         loop = asyncio.new_event_loop()
         asyncio.set_event_loop(loop)
     loop.run_until_complete(self._new_task(task_id))
Beispiel #14
0
Datei: cli.py Projekt: cirix/dcos
def run_loop(action, options):
    assert callable(action)
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    print_header('START {}'.format(action.__name__))
    try:
        config = backend.get_config()
        cli_delegate = CliDelegate()
        result = loop.run_until_complete(action(config, block=True, async_delegate=cli_delegate, options=options))
        pp = PrettyPrint(result)
        pp.stage_name = action.__name__
        pp.beautify('print_data')

    finally:
        loop.close()
    exitcode = 0
    for host_result in result:
        for command_result in host_result:
            for host, process_result in command_result.items():
                if process_result['returncode'] != 0:
                    exitcode += 1
    print_header('ACTION {} COMPLETE'.format(action.__name__))
    pp.print_summary()
    return exitcode
Beispiel #15
0
            def run_in_loop(s_args, s_kwargs, c_args, c_kwargs):

                loop = asyncio.new_event_loop()
                asyncio.set_event_loop(None)

                server = aioftp.Server(*s_args, loop=loop, **s_kwargs)
                client = aioftp.Client(*c_args, loop=loop, **c_kwargs)

                coro = asyncio.coroutine(f)
                try:

                    loop.run_until_complete(coro(loop, client, server))

                finally:

                    if hasattr(server, "server"):

                        server.close()
                        loop.run_until_complete(server.wait_closed())

                    if hasattr(client, "writer"):

                        client.close()

                    loop.close()
Beispiel #16
0
 def http(self):
     if self._parent:
         return self._parent.http
     else:
         if self._http is None:
             self._http = HttpClient(loop=asyncio.new_event_loop())
         return self._http
Beispiel #17
0
 def setUp(self):
     self.loop = asyncio.new_event_loop()
     asyncio.set_event_loop(None)
     self.database = 'aiopg'
     self.user = '******'
     self.host = '127.0.0.1'
     self.password = '******'
Beispiel #18
0
def test_worker_close(worker):
    loop = asyncio.new_event_loop()
    asyncio.sleep = mock.Mock(wraps=asyncio.coroutine(lambda *a, **kw: None))
    worker.ppid = 1
    worker.pid = 2
    worker.cfg.graceful_timeout = 1.0
    worker.signal = mock.Mock()
    worker.signal.stopped = False
    worker.wsgi = mock.Mock()
    conn = mock.Mock()
    conn.websocket = mock.Mock()
    conn.websocket.close_connection = mock.Mock(
        wraps=asyncio.coroutine(lambda *a, **kw: None)
    )
    worker.connections = set([conn])
    worker.log = mock.Mock()
    worker.loop = loop
    server = mock.Mock()
    server.close = mock.Mock(wraps=lambda *a, **kw: None)
    server.wait_closed = mock.Mock(
        wraps=asyncio.coroutine(lambda *a, **kw: None)
    )
    worker.servers = {server: {"requests_count": 14}}
    worker.max_requests = 10

    # close worker
    _close = asyncio.ensure_future(worker.close(), loop=loop)
    loop.run_until_complete(_close)

    assert worker.signal.stopped
    assert conn.websocket.close_connection.called
    assert len(worker.servers) == 0
Beispiel #19
0
 def setUp(self):
     self.transport = Mock()
     self.transport.write = self._write
     self.responses = []
     self._old_loop = asyncio.get_event_loop()
     self.loop = asyncio.new_event_loop()
     asyncio.set_event_loop(self.loop)
Beispiel #20
0
 def setUp(self):
     self._bot = make_mock_bot()
     self._store = make_clean_mongo_store()
     self._looper = FakeLooper()
     # http://stackoverflow.com/questions/23033939/how-to-test-python-3-4-asyncio-code
     self._loop = asyncio.new_event_loop()
     asyncio.set_event_loop(None)
Beispiel #21
0
def test_run_max_requests_exceeded(worker):
    loop = asyncio.new_event_loop()
    worker.ppid = 1
    worker.alive = True
    sock = mock.Mock()
    sock.cfg_addr = ("localhost", 8080)
    worker.sockets = [sock]
    worker.wsgi = mock.Mock()
    worker.connections = set()
    worker.log = mock.Mock()
    worker.loop = loop
    worker.servers = {
        "server1": {"requests_count": 14},
        "server2": {"requests_count": 15},
    }
    worker.max_requests = 10
    worker._run = mock.Mock(wraps=asyncio.coroutine(lambda *a, **kw: None))

    # exceeding request count
    _runner = asyncio.ensure_future(worker._check_alive(), loop=loop)
    loop.run_until_complete(_runner)

    assert not worker.alive
    worker.notify.assert_called_with()
    worker.log.info.assert_called_with(
        "Max requests exceeded, shutting " "down: %s", worker
    )
Beispiel #22
0
 def wrapper(*args, **kwargs):
     coroutine = asyncio.coroutine(function)
     loop = asyncio.new_event_loop()
     kwargs['loop'] = loop
     future = coroutine(*args, **kwargs)
     loop.run_until_complete(future)
     loop.close()
Beispiel #23
0
def loop():
    '''
    Keep things clean by using a new event loop
    '''
    loop = asyncio.new_event_loop()
    loop.set_debug(True)
    return loop
Beispiel #24
0
    def wrapper(**kwargs):

        @asyncio.coroutine
        def coroutine(loop, kwargs):
            redis = yield from find_connection(loop)
            if 'redis' in kwargs:
                kwargs['redis'] = redis
            if 'loop' in kwargs:
                kwargs['loop'] = loop
            try:
                yield from asyncio.coroutine(f)(**kwargs)
            except Exception:
                raise
            finally:
                yield from redis.flushdb()
                redis.close()
                yield from redis.wait_closed()

        loop = asyncio.new_event_loop()
        if 'set_loop' in kwargs:
            asyncio.set_event_loop(loop)
        else:
            asyncio.set_event_loop(None)
        loop.run_until_complete(coroutine(loop, kwargs))
        loop.stop()
        loop.run_forever()
        loop.close()
        gc.collect()
        asyncio.set_event_loop(None)
Beispiel #25
0
    def __init__(self, now_playing):
        config = ConfigParser()
        config.read(os.path.join('..', 'config.ini'))
        conf = {
            'discord': {
                'email': config.get('discord', 'email'),
                'pass': config.get('discord', 'pass'),
            },
        }

        print('Starting new event loop for discord api')
        loop = asyncio.new_event_loop()
        try:
            self.client = discord.Client(loop=loop)
            print('Discord client created')

            @self.client.event
            async def on_ready():
                print('Client ready')
                await self.client.change_status(game=discord.Game(name=now_playing))
                print('Status set')
                loop.run_until_complete(self.client.close())
                print('Shutting down client')

            self.client.run(conf['discord']['email'], conf['discord']['pass'])
        except Exception:
            print("Exception! Quitting")
            loop.run_until_complete(self.client.close())
        finally:
            print('Closing event loop')
            loop.close()
Beispiel #26
0
    def create_router(self):
        class MockController(object):
            def __init__(self, configuration, cache_factory, *args):
                pass

            @asyncio.coroutine
            def test_operation(self, parameter1: 'help1', parameter2: 'help2', parameter3: 'help3'='default'):
                """function help"""
                return parameter1 + parameter2 + parameter3

            @asyncio.coroutine
            def test_raise_controller_error(self):
                """raise 1"""
                raise colorcore.routing.ControllerError('Test error')

            @asyncio.coroutine
            def test_raise_transaction_builder_error(self):
                """raise 2"""
                raise openassets.transactions.InsufficientAssetQuantityError

        self.configuration = unittest.mock.Mock()
        self.output = io.StringIO()

        event_loop = asyncio.new_event_loop()

        create_server_return_value = asyncio.Future(loop=event_loop)
        create_server_return_value.set_result(None)
        event_loop.create_server = unittest.mock.Mock(
            spec=event_loop.create_server, return_value=create_server_return_value)
        return (
            colorcore.routing.Router(MockController, self.output, object, self.configuration, event_loop),
            event_loop)
Beispiel #27
0
    def test_lock_by_with_statement(self):
        loop = asyncio.new_event_loop()  # don't use TestLoop quirks
        self.set_event_loop(loop)
        primitives = [
            asyncio.Lock(loop=loop),
            asyncio.Condition(loop=loop),
            asyncio.Semaphore(loop=loop),
            asyncio.BoundedSemaphore(loop=loop),
        ]

        @asyncio.coroutine
        def test(lock):
            yield from asyncio.sleep(0.01, loop=loop)
            self.assertFalse(lock.locked())
            with self.assertWarns(DeprecationWarning):
                with (yield from lock) as _lock:
                    self.assertIs(_lock, None)
                    self.assertTrue(lock.locked())
                    yield from asyncio.sleep(0.01, loop=loop)
                    self.assertTrue(lock.locked())
                self.assertFalse(lock.locked())

        for primitive in primitives:
            loop.run_until_complete(test(primitive))
            self.assertFalse(primitive.locked())
 def _set_event_loop(self):
     if self.run_async:
         self._event_loop = asyncio.get_event_loop()
     else:
         loop = asyncio.new_event_loop()
         asyncio.set_event_loop(loop)
         self._event_loop = loop
Beispiel #29
0
        def run(self):
            """
                The thread function. This represents a new thread of execution.
            """
            while self.should_run is True:
                if self.discord_connection is not None:
                    self.discord_connection.logout()
                    print("!!! Attempting to reconnect.")

                self.loop = asyncio.new_event_loop()
                asyncio.set_event_loop(self.loop)

                self.discord_connection = discord.Client()

                @self.discord_connection.event
                @asyncio.coroutine
                def on_message(message):
                    if message.type is discord.MessageType.default:
                        self.outgoing_lock.acquire()
                        self.outgoing_messages.append(message)
                        self.outgoing_lock.release()

                try:
                    self.discord_connection.loop.create_task(self.process_input_messages())
                    self.discord_connection.run(self.configuration["token"])
                except Exception as e:
                    print("!!! Discord connection threw an exception: %s" % str(e))
Beispiel #30
0
def run(args):
    """Handle ensure configuration commandline script."""
    # Disable logging
    logging.getLogger('homeassistant.core').setLevel(logging.CRITICAL)

    parser = argparse.ArgumentParser(
        description=("Run a Home Assistant benchmark."))
    parser.add_argument('name', choices=BENCHMARKS)
    parser.add_argument('--script', choices=['benchmark'])

    args = parser.parse_args()

    bench = BENCHMARKS[args.name]

    print('Using event loop:', asyncio.get_event_loop_policy().__module__)

    with suppress(KeyboardInterrupt):
        while True:
            loop = asyncio.new_event_loop()
            hass = core.HomeAssistant(loop)
            hass.async_stop_track_tasks()
            runtime = loop.run_until_complete(bench(hass))
            print('Benchmark {} done in {}s'.format(bench.__name__, runtime))
            loop.run_until_complete(hass.async_stop())
            loop.close()

    return 0
Beispiel #31
0
 def make_pool(cls):
     pool = concurrent.futures.ThreadPoolExecutor(1)
     pool.submit(adjust_event_loop_policy).result()
     pool.submit(
         lambda: asyncio.set_event_loop(asyncio.new_event_loop())).result()
     return pool
Beispiel #32
0
 def init_loop(self):
     self.loop = asyncio.new_event_loop()
     asyncio.set_event_loop(self.loop)
     self.loop.queue = asyncio.Queue()
Beispiel #33
0
def run(
        provider,
        # AWS
        profile=None,
        aws_access_key_id=None,
        aws_secret_access_key=None,
        aws_session_token=None,
        # Azure
        user_account=False,
        service_account=None,
        cli=False,
        msi=False,
        service_principal=False,
        file_auth=None,
        tenant_id=None,
        subscription_id=None,
        client_id=None,
        client_secret=None,
        username=None,
        password=None,
        # GCP
        project_id=None,
        folder_id=None,
        organization_id=None,
        all_projects=False,
        # Aliyun
        access_key_id=None,
        access_key_secret=None,
        # General
        report_name=None,
        report_dir=None,
        timestamp=False,
        services=[],
        skipped_services=[],
        result_format='json',
        database_name=None,
        host_ip='127.0.0.1',
        host_port=8000,
        max_workers=10,
        regions=[],
        excluded_regions=[],
        fetch_local=False,
        update=False,
        max_rate=None,
        ip_ranges=[],
        ip_ranges_name_key='name',
        ruleset='default.json',
        exceptions=None,
        force_write=False,
        debug=False,
        quiet=False,
        log_file=None,
        no_browser=False,
        programmatic_execution=True):
    """
    Run a scout job in an async event loop.
    """

    loop = asyncio.get_event_loop()
    if loop.is_closed():
        loop = asyncio.new_event_loop()
    # Set the throttler within the loop so it's accessible later on
    loop.throttler = Throttler(rate_limit=max_rate if max_rate else 999999,
                               period=1)
    loop.set_default_executor(ThreadPoolExecutor(max_workers=max_workers))
    result = loop.run_until_complete(
        _run(**locals()))  # pass through all the parameters
    loop.close()
    return result
Beispiel #34
0
def run_client(my_configs_path,
               other_configs_path,
               num_of_commands=10,
               port=0):
    ''' Run the VASP's client to send commands to the other VASP.

    The VASP sends <num_of_commands> commands to the other VASP, on port <port>.
    If <port> is 0, the VASP defaults to the port specified in <other_configs>.
    Being able to easily modify the port allows to quickly test performance
    in different situations, such as HTTP, HTTPS, or custom port.

    The arguments <my_configs_path> and <other_configs_path> are paths to
    files describing the configurations of the current VASP and of the other
    VASP, respectively. Configs are dict taking the following form:
        configs = {
            'addr': <LibraAddress>,
            'base_url': <str>,
            'port': <int>,
        }
    '''
    assert num_of_commands > 0

    my_configs = load_configs(my_configs_path)
    other_configs = load_configs(other_configs_path)
    my_addr = my_configs['addr']
    other_addr = other_configs['addr']

    # Create VASP.
    vasp = Vasp(my_addr,
                host='0.0.0.0',
                port=my_configs['port'],
                business_context=TestBusinessContext(my_addr),
                info_context=SimpleVASPInfo(my_configs, other_configs, port),
                database={})
    logging.info(f'Created VASP {my_addr.as_str()}.')

    # Run VASP services.
    def start_services(vasp, loop):
        vasp.start_services()
        logging.debug('Start main loop.')
        try:
            loop.run_forever()
        finally:
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.close()

    loop = asyncio.new_event_loop()
    vasp.set_loop(loop)
    t = Thread(target=start_services, args=(vasp, loop), daemon=True)
    t.start()
    logging.info(f'VASP services are running on port {vasp.port}.')

    # Make a payment commands.
    commands = []
    for cid in range(num_of_commands):
        sub_a = LibraAddress.from_bytes(b'A' * 16, b'a' * 8).as_str()
        sub_b = LibraAddress.from_bytes(b'B' * 16, b'b' * 8).as_str()
        sender = PaymentActor(sub_b, StatusObject(Status.none), [])
        receiver = PaymentActor(sub_a, StatusObject(Status.none), [])
        action = PaymentAction(10, 'TIK', 'charge', '2020-01-02 18:00:00 UTC')
        reference = f'{my_addr.as_str()}_{cid}'
        payment = PaymentObject(sender, receiver, reference, 'orig_ref',
                                'desc', action)
        cmd = PaymentCommand(payment)
        commands += [cmd]

    # Send commands.
    logging.info(
        'Start measurements: '
        f'sending {num_of_commands} commands to {other_addr.as_str()}.')
    logging.info(
        f'The target URL is {vasp.info_context.get_peer_base_url(other_addr)}')
    start_time = time.perf_counter()

    async def send_commands(vasp, commands):
        return await asyncio.gather(
            *[vasp.new_command_async(other_addr, c) for c in commands],
            return_exceptions=True)

    res = asyncio.run_coroutine_threadsafe(send_commands(vasp, commands), loop)
    res = res.result()

    elapsed = (time.perf_counter() - start_time)

    # Display performance and success rate.
    success_number = sum([1 for r in res if r])
    logging.info(f'Commands executed in {elapsed:0.2f} seconds.')
    logging.info(f'Success #: {success_number}/{len(commands)}.')
    logging.info(f'Estimate throughput #: {len(commands)/elapsed} TPS.')
Beispiel #35
0
 def __init__(self, controller):
     self._controller = controller
     self._users = {}
     self._loop = asyncio.new_event_loop()
     asyncio.set_event_loop(self._loop)
def event_loop():
    """A module-scoped event loop."""
    return asyncio.new_event_loop()
Beispiel #37
0
 def __exit__(self, exc_type, exc_value, traceback):
     """Remove self from existing instances."""
     sys.path = self.stored_path
     self.__class__.instances = []
     asyncio.set_event_loop(asyncio.new_event_loop())
Beispiel #38
0
def delete_upload_async(request):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    result = loop.run_in_executor(None, delete_media_files)
    return redirect('collage:collage-form')
Beispiel #39
0
 def run(self):
     global LIST_LOOP
     LIST_LOOP = asyncio.new_event_loop()
     result = LIST_LOOP.run_until_complete(self.start_listener())
Beispiel #40
0
 def setUp(self):
     """Set up test class."""
     self.loop = asyncio.new_event_loop()
     asyncio.set_event_loop(self.loop)
Beispiel #41
0
def perspective_thread(manager):
    """Start a new perspective loop in this thread."""
    loop = asyncio.new_event_loop()
    manager.set_loop_callback(loop.call_soon_threadsafe)
    loop.run_forever()
Beispiel #42
0
 def __start(self):
     loop = asyncio.new_event_loop()
     loop.run_until_complete(self.receive_weights())
     loop.run_forever()
Beispiel #43
0
 def setUp(self):
     self.loop = asyncio.new_event_loop()
     asyncio.set_event_loop(None)
     self.handler = xss.XssEmulator()
Beispiel #44
0
    def main():
        loop = asyncio.new_event_loop()
        client = TelegramClient(
            'anon',
            settings.TELEGRAM_API_ID,
            settings.TELEGRAM_API_HASH,
            loop=loop).start(bot_token=settings.TELEGRAM_BOT_TOKEN)

        @client.on(events.CallbackQuery)
        async def callback(event):
            data = event.query.data.decode("utf-8").lower()
            print(data)
            cmd_out = BOX.commands(data)(event=event)
            # print(workpackages)
            # if(workpackages):
            #     print("true1")
            #     cmd_out= BOX.commands(data)(event,workpackages)
            #     print("true")
            # else:
            #     print("false")
            #     cmd_out= BOX.commands(data)(event)
            #     print("false")

            #workpackages = cmd_out[3]
            # print(event.query.user_id)
            # print(event.query.__dict__.keys())
            # print(event.query.data.decode("utf-8"))
            # print(BOX.commands(event.query.data.decode("utf-8").lower())()[0])
            # print(cmd_out)
            await client.edit_message(event.chat_id,
                                      event.query.msg_id,
                                      cmd_out[0],
                                      buttons=cmd_out[1])

        def funcname(parameter_list):
            pass

        @client.on(events.NewMessage(pattern='/start'))
        @client.on(events.NewMessage(pattern='/help'))
        async def start(event):
            """Send a message when the command /start is issued."""
            await event.respond(
                "Hi, I'm an audio slave! :3\nI would love to convert every wav you got into a telegram voice message. (>.<)"
            )
            raise events.StopPropagation

        @client.on(events.NewMessage(pattern='/init'))
        async def start(event):
            """Send a message when the command /start is issued."""
            cmd_out = BOX.commands('start')()
            print(cmd_out)
            await event.respond(cmd_out[0], buttons=cmd_out[1])

            raise events.StopPropagation

        @client.on(events.NewMessage)
        async def echo(event):
            """Echo the user message."""
            # if telegram message has attached a wav file, download and convert it
            if event.message.file and event.message.file.mime_type == 'audio/x-wav':
                msg = await event.respond("Processing...")

                try:
                    #start = time.time()

                    await msg.edit("**Downloading start...**")

                    # audio_in = io.BytesIO()
                    #audio_in.name = f"{event.message.file.name.split('.')[0]}_snek_{event.message.date.strftime('%m-%d_%H-%M')}.wav"
                    # audio_in = await client.download_media(event.message, audio_in, progress_callback=lambda d, t: asyncio.get_event_loop().create_task(
                    #     progress(d, t, msg, start)))

                    # audio_seg = AudioSegment.from_wav(audio_in)

                    # audio_out = io.BytesIO()
                    # audio_out = audio_seg.export(audio_out, bitrate="128k", format='ogg', codec="opus", parameters=["-strict", "-2", "-ac", "2", "-vol", "150"], tags={"ARTIST": "waveater", "GENRE": "Meeting/Trashtalk", "ALBUM": "waveater", "TITLE": f"{event.message.file.name.split('.')[0]}_{event.message.media.document.date.strftime('%m-%d_%H-%M')}", "DATE": f"{event.message.media.document.date.strftime('%Y/%m/%d_%H:%M:%S')}", "COMMENT": f"A wav file converted to telegram voice message.", "CONTACT":"@waveater"})
                    # audio_out.name = f"{event.message.file.name.split('.')[0]}_{event.message.media.document.date.strftime('%m-%d_%H-%M')}.ogg"
                    # print(len(audio_seg)//1000)
                    # print(event.message.file.id)
                    # print(event.message.file.title)
                    # print(event.message.file.performer)
                    # print(event.message.file.name)
                    # print(event.message.file.duration)
                    result = await client.send_file(
                        event.chat_id,
                        audio_out,
                        voice_note=True,
                        caption=
                        f"{event.message.message}\n\n`track: '{event.message.file.name.split('.')[0]}_{event.message.media.document.date.strftime('%m-%d_%H-%M')}',\nchannel: '{audio_seg.channels}'',\nformat: 'ogg',\ncodec: 'opus',\nbitrate: '128k'`",
                        reply_to=event.message)
                    # print(result.file.duration)
                    # print(result.file.performer)

                    await msg.delete()

                except Exception as e:
                    print(e)
                    await msg.edit(
                        f"OwO Shit happens! Something has gone wrong.\n\n**Error:** {e}"
                    )

        # print(f"{threading.enumerate()}")
        client.run_until_disconnected()
Beispiel #45
0
 def asyncio_thread(self):
     self.txloop = asyncio.new_event_loop()
     self.txloop.set_debug(True)
     asyncio.set_event_loop(self.txloop)
     asyncio.run_coroutine_threadsafe(self.setup(), self.txloop)
     self.txloop.run_forever()
 def thread_handler():
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     loop.run_until_complete(read_bytes_from_outside())
     loop.close()
Beispiel #47
0
def teardown_module(module):
    try:
        loop = asyncio.get_running_loop()
    except RuntimeError:
        loop = asyncio.new_event_loop()
    loop.run_until_complete(f.shutdown())
Beispiel #48
0
def get_stations_synchronously() -> List[WeatherStation]:
    """ Get list of stations - in a synchronous/blocking call.
    """
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    return loop.run_until_complete(get_stations_from_source(StationSourceEnum.LOCAL_STORAGE))
Beispiel #49
0
 def go(self, fn, args):
     return asyncio.new_event_loop().run_until_complete(fn(*args))
Beispiel #50
0
 def __init__(self, uri, client_class=Client, *args, **kwargs):
     super().__init__()
     LOGGER.info('Server URI: %s', uri)
     self._loop = asyncio.new_event_loop()
     self._client = client_class(uri, self._loop, *args, **kwargs)
     self.daemon = True
Beispiel #51
0
def run(
    coro: Coroutine,
    *,
    loop: Optional[asyncio.AbstractEventLoop] = None,
    debug: bool = False,
    set_to_none: bool = False,
) -> Any:
    """Run a |coroutine_link|_.

    This function runs the passed coroutine, taking care
    of the event loop and shutting down asynchronous generators.

    This function is basically ported from Python 3.7 for backwards compability
    with earlier versions of Python.

    This function cannot be called when another event loop is
    running in the same thread.

    If ``debug`` is ``True``, the event loop will be run in debug mode.

    This function creates a new event loop and closes it at the end if a ``loop`` is ``None``.

    If a loop is given, this function basically calls :meth:`asyncio.AbstractEventLoop.run_until_complete`.

    It should be used as a main entry point to asyncio programs, and should
    ideally be called only once.

    Example:

    .. code-block:: python3

        async def test(pid):
            return pid

        one = gd.utils.run(test(1))

    Parameters
    ----------
    coro: |coroutine_link|_
        Coroutine to run.

    loop: Optional[:class:`asyncio.AbstractEventLoop`]
        A loop to run ``coro`` with. If ``None`` or omitted, a new event loop is created.

    debug: :class:`bool`
        Whether or not to run event loop in debug mode.

    set_to_none: :class:`bool`
        Indicates if the loop should be set to None after execution.

    Returns
    -------
    `Any`
        Anything that ``coro`` returns.
    """
    if asyncio._get_running_loop() is not None:
        raise RuntimeError(
            "Can not perform gd.utils.run() in a running event loop.")

    if not asyncio.iscoroutine(coro):
        raise ValueError(f"A coroutine was expected, got {coro!r}.")

    shutdown = False

    if loop is None:
        loop = asyncio.new_event_loop()
        shutdown = True

    try:
        asyncio.set_event_loop(loop)
        loop.set_debug(debug)
        return loop.run_until_complete(coro)

    finally:
        if shutdown:
            shutdown_loop(loop)

        if set_to_none:
            loop = None

        else:
            loop = asyncio.new_event_loop()

        asyncio.set_event_loop(loop)
Beispiel #52
0
 def connect(self):
     """ """
     loop = asyncio.new_event_loop()
     self._event_thread = threading.Thread(target=self._run_loop, args=(loop,), daemon=True)
     self._event_thread.start()
     asyncio.run_coroutine_threadsafe(AioRobot.connect(self), loop).result()
 def run(self):
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     asyncio.get_event_loop().run_until_complete(self.send())
     asyncio.get_event_loop().close()
def pingloop():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    loop.run_until_complete(_pingloop())
    loop.close()
Beispiel #55
0
 def get_data_wrapper(*args, **kwargs):
     # New thread - get the loop.
     loop = asyncio.new_event_loop()
     asyncio.set_event_loop(loop)
     assert not loop.is_running()
     return loop.run_until_complete(fn(*args, **kwargs))
Beispiel #56
0
def deepfryer(bot: Bot, update: Update):
    global deepdata
    global deepdata2
    global deepdata3
    message = update.effective_message
    if message.reply_to_message:
        deepdata = message.reply_to_message.photo
        deepdata2 = message.reply_to_message.sticker
        deepdata3 = message.reply_to_message.animation
    else:
        deepdata = []
        deepdata2 = []
        deepdata3 = []

    # check if message does contain a photo and cancel when not
    if not deepdata and not deepdata2 and not deepdata3:
        message.reply_text("What am I supposed to do with this?!")
        return

    # download last photo (highres) as byte array
    if deepdata:
        photodata = deepdata[len(deepdata) -
                             1].get_file().download_as_bytearray()
        image = Image.open(io.BytesIO(photodata))
    elif deepdata2:
        sticker = bot.get_file(deepdata2.file_id)
        sticker.download('sticker.png')
        image = Image.open("sticker.png")
    elif deepdata3:
        global fps
        global imgno
        global chat_id
        chat_id = update.effective_chat.id
        message.reply_text("detected gif")
        gif = bot.get_file(deepdata3.file_id)
        gif.download('gif.mp4')
        if not Path('gifdata').is_dir():
            os.makedirs('gifdata')
        jsondata = ast.literal_eval(
            os.popen(
                'ffprobe -i gif.mp4 -v quiet -print_format json -show_format -show_streams -hide_banner'
            ).read())
        stringfps = jsondata["streams"][0]["avg_frame_rate"]
        templist = []
        for i in stringfps.split('/'):
            templist.append(float(i))
        fps = numpy.divide(templist[0], templist[1])
        if fps > 30.0:
            message.reply_text(
                "fps of the gif is too high! Make sure its less than or equal to 30! Reducing it to 20"
            )
            fps = 20.0
        filesize = float(os.path.getsize('gif.mp4'))
        if filesize > 5000000.0:
            message.reply_text("Filesize is larger than 5MB! not allowed!")
            deepdata3 = []
            return
        os.system('ffmpeg -i gif.mp4 -r {} gifdata/out%05d.jpg'.format(fps))
        duration = float(jsondata["streams"][0]["duration"])
        imgno = int(duration * fps)
        image = 0

    # the following needs to be executed async (because dumb lib)
    loop = asyncio.new_event_loop()
    loop.run_until_complete(
        process_deepfry(image, message.reply_to_message, bot))
    loop.close()
import asyncio
import sys

loop = asyncio.new_event_loop()

print(loop)
asyncio.set_event_loop(loop) 

import pdb; pdb.set_trace()
if sys.platform != "win32":
    watcher = asyncio.get_child_watcher()
    # a gotcha --> must attach to the newly created loop to the event loop policy's watcher
    # to make sure that our event loop monitors the termination of newly spawned subprocesses on UNIX systems. 
    watcher.attach_loop(loop)
Beispiel #58
0
    def __init__(self):
        super().__init__(name="NetworkThread")
        # There is only one event loop and no more than one thread must be created
        assert not self.network_event_loop

        NetworkThread.network_event_loop = asyncio.new_event_loop()
Beispiel #59
0
 def setUp(self):
     self.loop = asyncio.new_event_loop()
     self.loop.set_debug(False)
     asyncio.set_event_loop(self.loop)
     self.devices = self.load_credits()
     self.assertFalse(len(self.devices) == 0)
Beispiel #60
0
async def requestandSend():
   while True:
      try : 
         inputSend()
      except IOError:
         #sys.exit()  #Terminate the program 
         print("err")

def start_loop(loop):
   asyncio.set_event_loop(loop)
   loop.run_forever()


   ##Essentially the main()
 ##if you want to update a global within a function declare as global at start of function 
   
loop1 = asyncio.new_event_loop()
t1 = Thread(target=start_loop, args=(loop1,))
loop2 = asyncio.new_event_loop()
t2 = Thread(target=start_loop, args=(loop2,))

t1.start()  ##2 threads running in parallel
t2.start()


asyncio.run_coroutine_threadsafe(receiveandPrint(),loop1)
asyncio.run_coroutine_threadsafe(requestandSend(),loop2)