Ejemplo n.º 1
0
def run_service(*args, **kwargs) -> None:
    if uvloop is not None:
        uvloop.install()
    return asyncio.run(async_run_service(*args, **kwargs))
Ejemplo n.º 2
0
#!/usr/bin/env python

import time
import asyncio
from datetime import datetime
import zmq

from rpc_agent import FreelanceClient

import uvloop

uvloop.install()

ZMQ_AGAIN = zmq.Again

# sudo apt-get install libzmq3-dev
# sudo apt-get install libzmq5
#
# 194K req/s with 6 clients x 100 000 requests GO
# 156K req/s with 6 clients x 600 000 requests GO
#
# 180K req/s with 1 client x 600 000 requests CPython
# 106K req/s with 6 clients x 600 000 requests CPython
#
# 194K req/s with 1 client x 600 000 requests PyPy
# 85K req/s with 6 clients x 600 000 requests PyPy
#
# QOS
#   remove @TODO
#   more fair load balancing
#   fixer l'arrêt d'un seul server
Ejemplo n.º 3
0
def main():
    if uvloop is not None:
        uvloop.install()
    asyncio.run(start_websocket_server())
Ejemplo n.º 4
0
def main(
    cli_ctx: click.Context,
    config_path: Path,
    debug: bool,
) -> int:

    # Determine where to read configuration.
    raw_cfg, cfg_src_path = config.read_from_file(config_path, 'agent')

    # Override the read config with environment variables (for legacy).
    config.override_with_env(raw_cfg, ('etcd', 'namespace'), 'BACKEND_NAMESPACE')
    config.override_with_env(raw_cfg, ('etcd', 'addr'), 'BACKEND_ETCD_ADDR')
    config.override_with_env(raw_cfg, ('etcd', 'user'), 'BACKEND_ETCD_USER')
    config.override_with_env(raw_cfg, ('etcd', 'password'), 'BACKEND_ETCD_PASSWORD')
    config.override_with_env(raw_cfg, ('agent', 'rpc-listen-addr', 'host'),
                             'BACKEND_AGENT_HOST_OVERRIDE')
    config.override_with_env(raw_cfg, ('agent', 'rpc-listen-addr', 'port'),
                             'BACKEND_AGENT_PORT')
    config.override_with_env(raw_cfg, ('agent', 'pid-file'), 'BACKEND_PID_FILE')
    config.override_with_env(raw_cfg, ('container', 'port-range'),
                             'BACKEND_CONTAINER_PORT_RANGE')
    config.override_with_env(raw_cfg, ('container', 'kernel-host'),
                             'BACKEND_KERNEL_HOST_OVERRIDE')
    config.override_with_env(raw_cfg, ('container', 'sandbox-type'), 'BACKEND_SANDBOX_TYPE')
    config.override_with_env(raw_cfg, ('container', 'scratch-root'), 'BACKEND_SCRATCH_ROOT')
    if debug:
        config.override_key(raw_cfg, ('debug', 'enabled'), True)
        config.override_key(raw_cfg, ('logging', 'level'), 'DEBUG')
        config.override_key(raw_cfg, ('logging', 'pkg-ns', 'ai.backend'), 'DEBUG')

    # Validate and fill configurations
    # (allow_extra will make configs to be forward-copmatible)
    try:
        cfg = config.check(raw_cfg, agent_local_config_iv)
        if cfg['agent']['backend'] == AgentBackend.KUBERNETES:
            cfg = config.check(raw_cfg, k8s_extra_config_iv)
            if cfg['registry']['type'] == 'local':
                registry_target_config_iv = registry_local_config_iv
            elif cfg['registry']['type'] == 'ecr':
                registry_target_config_iv = registry_ecr_config_iv
            else:
                print('Validation of agent configuration has failed: registry type {} not supported'
                    .format(cfg['registry']['type']), file=sys.stderr)
                raise click.Abort()

            registry_cfg = config.check(cfg['registry'], registry_target_config_iv)
            cfg['registry'] = registry_cfg
        if cfg['agent']['backend'] == AgentBackend.DOCKER:
            config.check(raw_cfg, docker_extra_config_iv)
        if 'debug' in cfg and cfg['debug']['enabled']:
            print('== Agent configuration ==')
            pprint(cfg)
        cfg['_src'] = cfg_src_path
    except config.ConfigurationError as e:
        print('ConfigurationError: Validation of agent configuration has failed:', file=sys.stderr)
        print(pformat(e.invalid_data), file=sys.stderr)
        raise click.Abort()

    rpc_host = cfg['agent']['rpc-listen-addr'].host
    if (isinstance(rpc_host, BaseIPAddress) and
        (rpc_host.is_unspecified or rpc_host.is_link_local)):
        print('ConfigurationError: '
              'Cannot use link-local or unspecified IP address as the RPC listening host.',
              file=sys.stderr)
        raise click.Abort()

    if os.getuid() != 0 and cfg['container']['stats-type'] == 'cgroup':
        print('Cannot use cgroup statistics collection mode unless the agent runs as root.',
              file=sys.stderr)
        raise click.Abort()

    if cli_ctx.invoked_subcommand is None:

        if cfg['debug']['coredump']['enabled']:
            if not sys.platform.startswith('linux'):
                print('ConfigurationError: '
                      'Storing container coredumps is only supported in Linux.',
                      file=sys.stderr)
                raise click.Abort()
            core_pattern = Path('/proc/sys/kernel/core_pattern').read_text().strip()
            if core_pattern.startswith('|') or not core_pattern.startswith('/'):
                print('ConfigurationError: '
                      '/proc/sys/kernel/core_pattern must be an absolute path '
                      'to enable container coredumps.',
                      file=sys.stderr)
                raise click.Abort()
            cfg['debug']['coredump']['core_path'] = Path(core_pattern).parent

        cfg['agent']['pid-file'].write_text(str(os.getpid()))
        log_sockpath = Path(f'/tmp/backend.ai/ipc/agent-logger-{os.getpid()}.sock')
        log_sockpath.parent.mkdir(parents=True, exist_ok=True)
        log_endpoint = f'ipc://{log_sockpath}'
        cfg['logging']['endpoint'] = log_endpoint
        try:
            logger = Logger(cfg['logging'], is_master=True, log_endpoint=log_endpoint)
            with logger:
                ns = cfg['etcd']['namespace']
                setproctitle(f"backend.ai: agent {ns}")
                log.info('Backend.AI Agent {0}', VERSION)
                log.info('runtime: {0}', utils.env_info())

                log_config = logging.getLogger('ai.backend.agent.config')
                if debug:
                    log_config.debug('debug mode enabled.')

                if cfg['agent']['event-loop'] == 'uvloop':
                    import uvloop
                    uvloop.install()
                    log.info('Using uvloop as the event loop backend')
                aiotools.start_server(
                    server_main_logwrapper,
                    num_workers=1,
                    args=(cfg, log_endpoint),
                )
                log.info('exit.')
        finally:
            if cfg['agent']['pid-file'].is_file():
                # check is_file() to prevent deleting /dev/null!
                cfg['agent']['pid-file'].unlink()
    else:
        # Click is going to invoke a subcommand.
        pass
    return 0
Ejemplo n.º 5
0
 def __init__(self, debug=False):
   if not debug:
     uvloop.install()
   self.loop = asyncio.get_event_loop()
   self.prepared = False
Ejemplo n.º 6
0
                                                           'TRUE', 'T', 't',
                                                           'ok', 'Ok', 'OK',
                                                           '1') else False
if __debug_mode:
    print(f'------ Moca Modules ({__version}) was loaded. ------')
del __version

# set process name.
if __PROCESS_NAME__ is not None:
    setproctitle(str(__PROCESS_NAME__))

# setup uvloop
if platform != 'win32' and platform != 'cygwin':
    try:
        from uvloop import install
        install()
        if __debug_mode:
            print(f'------ Setup uvloop successfully. ------')
    except (ImportError, ModuleNotFoundError):
        print(f'------ Setup uvloop failed. ------')
del __debug_mode


# run at exit
def exit_func():
    try:
        from . import atexit as _
    except (ImportError, ModuleNotFoundError):
        pass

Ejemplo n.º 7
0
def run_app(port: int, **kwargs) -> None:
    uvloop.install()
    web.run_app(app=Application(), host='0.0.0.0', port=port, **kwargs)
Ejemplo n.º 8
0
def init_uvloop():
    import uvloop

    uvloop.install()
Ejemplo n.º 9
0
    If a value isn't found in one of the three locations, the configuration item will
    raise an error.
 """

from typing import Dict

import uvloop
from starlette.config import Config
from starlette.datastructures import Secret

from schemas.database_url import DatabaseURL
from util.toml import project, version  # noqa

# --- uvloop ----------------------------------------------------------------- #

uvloop.install()  # configure asyncio to use uvloop as the default event loop

# --- general ---------------------------------------------------------------- #

conf: Config = Config(env_file=".env")  # initialize from .env file, if present

ENV: str = conf("ENV", cast=str, default="development")
DEBUG: bool = conf("DEBUG", cast=bool, default=False)

# --- backends --------------------------------------------------------------- #

DATABASE_DRIVER: str = conf("DATABASE_DRIVER",
                            cast=str,
                            default="postgresql+asyncpg")
DATABASE_USERNAME: str = conf("DATABASE_USERNAME", cast=str)
DATABASE_PASSWORD: Secret = conf("DATABASE_PASSWORD", cast=Secret)
Ejemplo n.º 10
0
def main() -> None:
    uvloop.install()
    asyncio.run(async_main())
Ejemplo n.º 11
0
def controller_main():
    """The routine that kicks things off including arg parsing
    """
    parser = argparse.ArgumentParser()

    # Get supported output, 'gather' cannot be manually selected
    supported_outputs = OutputWorker.get_plugins()
    if supported_outputs.get('gather', None):
        del supported_outputs['gather']
    supported_outputs = list(supported_outputs)

    # Two inputs are possible:
    # 1. Suzieq inventory file
    # 2. Input directory
    source_arg = parser.add_mutually_exclusive_group()
    source_arg.add_argument('-I',
                            '--inventory',
                            type=str,
                            help='Input inventory file')

    source_arg.add_argument(
        '-i',
        '--input-dir',
        type=str,
        help=('Directory where run-once=gather data is. Process the data in '
              'directory as they were retrieved by the hosts'))

    parser.add_argument('-c',
                        '--config',
                        help='Controller configuration file',
                        type=str)

    parser.add_argument(
        '--debug',
        action='store_true',
        help='Build the node list and exit without polling the nodes')

    parser.add_argument(
        '-x',
        '--exclude-services',
        type=str,
        help='Exclude running this space separated list of services',
    )

    parser.add_argument(
        '--no-coalescer',
        default=False,
        action='store_true',
        help='Do not start the coalescer',
    )

    parser.add_argument(
        '-o',
        '--outputs',
        nargs='+',
        default=['parquet'],
        choices=supported_outputs,
        type=str,
        help='Output formats to write to: parquet. Use '
        'this option multiple times for more than one output',
    )

    parser.add_argument(
        "--output-dir",
        type=str,
        default=f'{os.path.abspath(os.curdir)}/sqpoller-output',
        help=argparse.SUPPRESS,
    )

    parser.add_argument(
        '--run-once',
        type=str,
        choices=['gather', 'process', 'update'],
        help=('''The poller do not run forever, three modes are available:
        (1) gather: store the output as it has been collected,
        (2) process: performs some processing on the data.
        Both cases store the results in a plain output file,
        one for each service, and exit.
        (3) update: poll the nodes only once, write the result and stop'''))

    parser.add_argument(
        '-s',
        '--service-only',
        type=str,
        help='Only run this space separated list of services',
    )

    parser.add_argument('--ssh-config-file',
                        type=str,
                        default=None,
                        help='Path to ssh config file, that you want to use')

    parser.add_argument(
        '-p',
        '--update-period',
        help='How frequently the inventory updates [DEFAULT=3600]',
        type=int)

    parser.add_argument(
        '-w',
        '--workers',
        type=int,
        help='The number of workers polling the nodes',
    )

    parser.add_argument('-V',
                        '--version',
                        action='store_true',
                        help='Print suzieq version')

    args = parser.parse_args()

    if args.version:
        print_version()
        sys.exit(0)

    uvloop.install()
    cfg = load_sq_config(config_file=args.config)
    if not cfg:
        print("Could not load config file, aborting")
        sys.exit(1)

    try:
        asyncio.run(start_controller(args, cfg))
    except (KeyboardInterrupt, RuntimeError):
        pass
    except Exception:  # pylint: disable=broad-except
        traceback.print_exc()
Ejemplo n.º 12
0
    def run(self, appdaemon, hadashboard, admin, aui, api, http):
        """ Start AppDaemon up after initial argument parsing.

        Args:
            appdaemon: Config for AppDaemon Object.
            hadashboard: Config for HADashboard Object.
            admin: Config for admin Object.
            aui: Config for aui Object.
            api: Config for API Object
            http: Config for HTTP Object

        Returns:
            None.

        """

        try:

            # if to use uvloop
            if appdaemon.get("uvloop") is True and uvloop:
                self.logger.info("Running AD using uvloop")
                uvloop.install()

            loop = asyncio.get_event_loop()

            # Initialize AppDaemon

            self.AD = ad.AppDaemon(self.logging, loop, **appdaemon)

            # Initialize Dashboard/API/admin

            if http is not None and (hadashboard is not None
                                     or admin is not None or aui is not None
                                     or api is not False):
                self.logger.info("Initializing HTTP")
                self.http_object = adhttp.HTTP(
                    self.AD,
                    loop,
                    self.logging,
                    appdaemon,
                    hadashboard,
                    admin,
                    aui,
                    api,
                    http,
                )
                self.AD.register_http(self.http_object)
            else:
                if http is not None:
                    self.logger.info(
                        "HTTP configured but no consumers are configured - disabling"
                    )
                else:
                    self.logger.info("HTTP is disabled")

            self.logger.debug("Start Main Loop")

            pending = asyncio.all_tasks(loop)
            loop.run_until_complete(asyncio.gather(*pending))

            #
            # Now we are shutting down - perform any necessary cleanup
            #

            self.AD.terminate()

            self.logger.info("AppDaemon is stopped.")

        except Exception:
            self.logger.warning("-" * 60)
            self.logger.warning("Unexpected error during run()")
            self.logger.warning("-" * 60, exc_info=True)
            self.logger.warning("-" * 60)

            self.logger.debug("End Loop")

            self.logger.info("AppDaemon Exited")
Ejemplo n.º 13
0
def cli():
    """Main cli entrypoint."""
    uvloop.install()
Ejemplo n.º 14
0
def main() -> None:
    uvloop.install()
    runner = cli(Erasmus, './config.toml')
    runner()
Ejemplo n.º 15
0
def transaction(topic_name, topic_text, parts_list):
    async def perform_http_request(client: HttpClient, url: str, data: str,
                                   print_response: bool) -> None:
        # perform request
        start = time.time()
        if data is not None:
            http_events = await client.post(
                url,
                data=data.encode(),
                headers={"content-type": "application/x-www-form-urlencoded"},
            )
        else:
            http_events = await client.get(url)
        elapsed = time.time() - start

        # print speed
        octets = 0
        for http_event in http_events:
            if isinstance(http_event, DataReceived):
                octets += len(http_event.data)
        logger.info("Received %d bytes in %.1f s (%.3f Mbps)" %
                    (octets, elapsed, octets * 8 / elapsed / 1000000))

        # print response
        if print_response:
            for http_event in http_events:
                if isinstance(http_event, HeadersReceived):
                    headers = b""
                    for k, v in http_event.headers:
                        headers += k + b": " + v + b"\r\n"
                    if headers:
                        sys.stderr.buffer.write(headers + b"\r\n")
                        sys.stderr.buffer.flush()
                elif isinstance(http_event, DataReceived):
                    sys.stdout.buffer.write(http_event.data)
                    sys.stdout.buffer.flush()

    def save_session_ticket(ticket):
        """
        Callback which is invoked by the TLS engine when a new session ticket
        is received.
        """
        logger.info("New session ticket received")
        if args.session_ticket:
            with open(args.session_ticket, "wb") as fp:
                pickle.dump(ticket, fp)

    async def run(
        configuration: QuicConfiguration,
        url: str,
        data: str,
        parallel: int,
        print_response: bool,
    ) -> None:
        # parse URL
        parsed = urlparse(url)
        assert parsed.scheme in (
            "https",
            "wss",
        ), "Only https:// or wss:// URLs are supported."
        if ":" in parsed.netloc:
            host, port_str = parsed.netloc.split(":")
            port = int(port_str)
        else:
            host = parsed.netloc
            port = 443

        async with connect(
                host,
                port,
                configuration=configuration,
                create_protocol=HttpClient,
                session_ticket_handler=save_session_ticket,
        ) as client:
            client = cast(HttpClient, client)

            if parsed.scheme == "wss":
                ws = await client.websocket(url,
                                            subprotocols=["chat", "superchat"])

                # print("Hint: To send a message type and press enter.")

                # ******************************
                # *** ASYNCHRONOUS THREADING ***
                def start_loop(loop):
                    asyncio.set_event_loop(loop)
                    loop.run_forever()

                new_loop = asyncio.new_event_loop()
                t = Thread(target=start_loop, args=(new_loop, ))
                t.start()

                async def read_user():
                    # while True:
                    message = add_item(topic_name, topic_text)
                    # message = add_item()
                    await ws.send(message)
                    # CheckLogin()

                asyncio.run_coroutine_threadsafe(read_user(), new_loop)

                # *** STAYS IN MAIN LOOP ***
                while True:
                    messageRec = await ws.recv()
                    print("< " + messageRec)
                    if messageRec == "inserted_item":
                        print("client update list")
                        root = Tk()
                        app = App(root)
                        app.update_text()
                        root.mainloop()

                # *** ASYNCHRONOUS THREADING ***
                # ******************************'''

                await ws.close()
            else:
                # perform request
                coros = [
                    perform_http_request(client=client,
                                         url=url,
                                         data=data,
                                         print_response=print_response)
                    for i in range(parallel)
                ]
                await asyncio.gather(*coros)

    if __name__ == "__main__":
        parser = argparse.ArgumentParser(description="HTTP/3 client")
        parser.add_argument("url",
                            type=str,
                            help="the URL to query (must be HTTPS)")
        parser.add_argument(
            "--ca-certs",
            type=str,
            help="load CA certificates from the specified file")
        parser.add_argument("-d",
                            "--data",
                            type=str,
                            help="send the specified data in a POST request")
        parser.add_argument(
            "-k",
            "--insecure",
            action="store_true",
            help="do not validate server certificate",
        )
        parser.add_argument("--legacy-http",
                            action="store_true",
                            help="use HTTP/0.9")
        parser.add_argument("-q",
                            "--quic-log",
                            type=str,
                            help="log QUIC events to a file in QLOG format")
        parser.add_argument(
            "-l",
            "--secrets-log",
            type=str,
            help="log secrets to a file, for use with Wireshark",
        )
        parser.add_argument("--parallel",
                            type=int,
                            default=1,
                            help="perform this many requests in parallel")
        parser.add_argument("--print-response",
                            action="store_true",
                            help="print response headers and body")
        parser.add_argument(
            "-s",
            "--session-ticket",
            type=str,
            help="read and write session ticket from the specified file",
        )
        parser.add_argument("-v",
                            "--verbose",
                            action="store_true",
                            help="increase logging verbosity")

        args = parser.parse_args()

        logging.basicConfig(
            format="%(asctime)s %(levelname)s %(name)s %(message)s",
            level=logging.DEBUG if args.verbose else logging.INFO,
        )

        # prepare configuration
        configuration = QuicConfiguration(
            is_client=True,
            alpn_protocols=H0_ALPN if args.legacy_http else H3_ALPN)
        if args.ca_certs:
            configuration.load_verify_locations(args.ca_certs)
        if args.insecure:
            configuration.verify_mode = ssl.CERT_NONE
        if args.quic_log:
            configuration.quic_logger = QuicLogger()
        if args.secrets_log:
            configuration.secrets_log_file = open(args.secrets_log, "a")
        if args.session_ticket:
            try:
                with open(args.session_ticket, "rb") as fp:
                    configuration.session_ticket = pickle.load(fp)
            except FileNotFoundError:
                pass

        if uvloop is not None:
            uvloop.install()
        loop = asyncio.get_event_loop()
        try:
            loop.run_until_complete(
                run(
                    configuration=configuration,
                    url=args.url,
                    data=args.data,
                    parallel=args.parallel,
                    print_response=args.print_response,
                ))
        finally:
            if configuration.quic_logger is not None:
                with open(args.quic_log, "w") as logger_fp:
                    json.dump(configuration.quic_logger.to_dict(),
                              logger_fp,
                              indent=4)
Ejemplo n.º 16
0
def main():
    if uvloop is not None:
        uvloop.install()
    asyncio.run(async_main())
Ejemplo n.º 17
0
def build_app(db_uri=None):
    """Sets up the app and installs uvloop."""
    app = web.Application(
        middlewares=[SentryMiddleware(), validate_token_middleware])
    uvloop.install()

    # decide which payment handler to use
    if stripe_key is None:
        logger.warn("No stripe key provided! Not charging customers.")
        payment_manager = DummyPaymentManager
    else:
        payment_manager = PaymentManager

    initialize_firebase()

    if server_mode == "development":
        verifier = DummyVerifier()
    else:
        verifier = FirebaseVerifier("dragorhast-420")

    # keep a track of all open bike connections
    app['payment_manager'] = payment_manager(stripe_key)
    app['bike_location_manager'] = BikeConnectionManager()
    app['rental_manager'] = RentalManager(app['payment_manager'])
    app['reservation_manager'] = ReservationManager(
        app['bike_location_manager'], app['rental_manager'])
    app['reservation_sourcer'] = ReservationSourcer(app['reservation_manager'])
    app['statistics_reporter'] = StatisticsReporter(app['rental_manager'],
                                                    app['reservation_manager'])
    app['database_uri'] = db_uri if db_uri is not None else 'spatialite://:memory:'
    app['token_verifier'] = verifier

    # set up the background tasks
    register_signals(app)

    # register views
    register_views(app, api_root)
    app.router.add_get("/", redoc)
    app.router.add_get("/logo.svg", logo)

    setup_aiohttp_apispec(
        app=app,
        title=name,
        version=__version__,
        url=f"{api_root}/schema",
        servers=[{
            "url": "http://api.tap2go.co.uk"
        }],
        info={"x-logo": {
            "url": "/logo.svg",
            "altText": "tap2go logo"
        }},
        externalDocs={
            "description": "Tap2Go Software Docs",
            "url": "https://tap2go-server.netlify.com/"
        },
        components={
            "securitySchemes": {
                "FirebaseToken": {
                    "type": "http",
                    "description": "A valid firebase token JWT",
                    "scheme": "bearer",
                    "bearerFormat": "JWT",
                }
            }
        },
    )

    # set up sentry exception tracking
    if server_mode != "development":
        logger.info("Starting Sentry Logging")
        sentry_sdk.init(
            dsn="https://[email protected]/1296249",
            environment=server_mode,
            release=f"server@{__version__}")

    if server_mode == "development" or server_mode == "testing":
        asyncio.get_event_loop().set_debug(True)

    return app
Ejemplo n.º 18
0
 def run(self):
     uvloop.install()
     asyncio.run(self.serve())
Ejemplo n.º 19
0
 def multiprocessing_func(accept_socket):
     uvloop.install()
     asyncio.run(func(accept_socket))
Ejemplo n.º 20
0
#!/usr/local/bin/python3.6

import os  # NOQA: E402

import uvloop  # NOQA: E402

os.environ['CORE_CONFIG'] = '/var/www/config.py'  # NOQA: E402
print("config path: ", os.environ['CORE_CONFIG'])
uvloop.install()  # NOQA: E402

from executors.driver.driver_executor import DriverExecutor
from executors.eld.eld_executor import EldExecutor
from executors.groups.groups_executor import GroupsExecutor
from executors.terminal.terminal_executor import TerminalExecutor
from executors.vehicle.vehicle_executor import VehicleExecutor
from executors.fuel_type.fuel_type_exe import FuelTypeExecutor
from executors.issue_states.issue_state_exe import IssueStateExecutor
from executors.unit.unit_executor import UnitExecutor
from executors.user.user_executor import UserExecutor
# import sys
# sys.path.append(os.path.abspath(__file__ + "/../modules/iwpp/"))

# print('>>>%s' % (sys.path))

from modules.core import Logger
from modules.core.QueueListener import QueueListener

from executors.driving_event.driving_event_executor import *

main_loop = asyncio.get_event_loop()
def _run_coins_withdrawal(**kwargs):
    uvloop.install()
    asyncio.run(CoinsWithdrawal(**kwargs).coins_withdrawal())
Ejemplo n.º 22
0
def main():
    uvloop.install()
    logs.setup_logging(worker="streams")
    asyncio.run(run_forever())
Ejemplo n.º 23
0
def run():
    uvloop.install()
    client.run(config.token)
Ejemplo n.º 24
0
def main_async(queues, result):
    # another implementation of event loop engine, It's faster than asyncio implementation, to use the asyncio
    # implementation just comment uvloop.install()
    uvloop.install()
    # asyncio.run execute the event loop, It just casualty the method's name is event_loop :p
    asyncio.run(event_loop(queues, result))
Ejemplo n.º 25
0
def cherrydoor():
    """Run the server."""
    parser = argparse.ArgumentParser(prog="cherrydoor",
                                     description="Cherrydoor management")
    parser.add_argument(
        "-v",
        "--version",
        help="Print Cherrydoor version and exit",
        action="version",
        version=f"Cherrydoor {__version__}",
    )
    subparsers = parser.add_subparsers(dest="subcommand")
    install_parser = subparsers.add_parser(
        "install", help="Install some possible requirements")
    install_parser.set_defaults(install_steps_excluded=[],
                                install_steps=[],
                                fail=False)
    install_parser.add_argument(
        "--exit-on-fail",
        help="If any step fails, stop the installer",
        dest="fail",
        action="store_true",
    )
    install_steps_group = install_parser.add_argument_group(
        "steps",
        "installation steps you want to run (if none are selected all will be run)",
    )
    install_steps = {
        "dependencies":
        "install all dependencies that weren't installed with pip",
        "service": "set up a systemd unit file",
        "config": "create a config file",
        "database": "set up MongoDB user, collections, etc.",
        "user": "******",
    }
    for (step, description) in install_steps.items():
        install_steps_group.add_argument(
            f"--{step}",
            dest="install_steps",
            action="append_const",
            const=step,
            help=description,
        )
        install_steps_group.add_argument(
            f"--no-{step}",
            dest="install_steps_excluded",
            action="append_const",
            const=step,
            help=f"don't {description}",
        )
    update_parser = subparsers.add_parser(
        "update", help="update Cherrydoor to the newest version")
    update_parser.set_defaults(update_steps_excluded=[],
                               update_steps=[],
                               fail=False)
    update_parser.add_argument(
        "-d",
        "--dev",
        help="Install latest development version",
        dest="dev",
        action="store_true",
    )
    update_parser.add_argument(
        "-v",
        "--verison",
        help="Install specific version",
        dest="version",
        action="store",
    )
    update_parser.add_argument(
        "--exit-on-fail",
        help="If any step fails, stop the updater",
        dest="fail",
        action="store_true",
    )
    update_steps_group = update_parser.add_argument_group(
        "steps",
        "update steps you want to run (if none are selected all will be run)",
    )
    update_steps = {
        "pip": "install the newest version of Cherrydoor via pip",
        "config": "update your config file",
        "database": "Update database schema and settings",
    }
    for (step, description) in update_steps.items():
        update_steps_group.add_argument(
            f"--{step}",
            dest="update_steps",
            action="append_const",
            const=step,
            help=description,
        )
        update_steps_group.add_argument(
            f"--no-{step}",
            dest="update_steps_excluded",
            action="append_const",
            const=step,
            help=f"don't {description}",
        )

    start_parser = subparsers.add_parser(
        "start",
        help=
        "Explicitly start the server (this action is preformed if no other argument is passed too)",
    )
    add_args(parser)
    add_args(start_parser)
    args = parser.parse_args()
    config, _ = load_config(args)
    log_level = getattr(logging, config.get("log_level", "WARN").upper())
    if not isinstance(log_level, int):
        log_level = logging.WARN
        logging.warn(
            "Invalid log level %s - defaulting to WARN",
            config.get("log_level", "WARN").upper(),
        )
    logging.basicConfig(
        level=log_level,
        format="%(asctime)s:%(name)s:%(levelname)s: %(message)s",
    )
    if args.subcommand == "install":
        from cherrydoor.cli.install import install

        install(args)
    if args.subcommand == "update":
        from cherrydoor.cli.update import update

        update(args)
    # if start argument was passed or no arguments were used, start the server
    if args.subcommand in ["start", None]:
        from cherrydoor.app import setup_app
        from cherrydoor.interface.serial import Serial

        try:
            import uvloop

            uvloop.install()
        except ModuleNotFoundError:
            pass
        loop = asyncio.get_event_loop()
        app = setup_app(loop, config)
        interface = Serial(app["db"], loop)
        app["serial"] = interface
        app.on_startup.append(interface.aiohttp_startup)
        app.on_cleanup.append(interface.cleanup)

        web.run_app(
            app,
            host=config.get("host", "127.0.0.1"),
            port=config.get("port", 5000),
            path=config.get("path", None),
        )
Ejemplo n.º 26
0
 def setUp(self):
     uvloop.install()
Ejemplo n.º 27
0
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import annotations

if __name__ == "__main__":
    try:
        import pyjion  # type: ignore

        pyjion.enable()  # type: ignore
        pyjion.config(threshold=30,
                      pgc=True,
                      level=2,
                      debug=False,
                      graph=False)  # type: ignore
        print("Running with pyjion")

    except ImportError:
        print("Running without pyjion")
        pass

    try:
        import uvloop  # type: ignore

        print("Running with uvloop")
        uvloop.install()  # type: ignore

    except ImportError:
        print("Running with standard asyncio loop")

    import reinhard.cli
    reinhard.cli.main()
Ejemplo n.º 28
0
def run_worker(sockname):
    uvloop.install()
    with devmode.CoverageConfig.enable_coverage_if_requested():
        asyncio.run(worker(sockname))
Ejemplo n.º 29
0
def run():
    uvloop.install()
    with suppress(KeyboardInterrupt, SystemExit):
        asyncio.run(_run_servers())
Ejemplo n.º 30
0
import os
import sys
import traceback
import asyncio
import concurrent.futures
import uvloop
from kubernetes_asyncio import client, config

timeout_seconds = int(sys.argv[1])
namespace = sys.argv[2]
name = sys.argv[3]

print(f'info: wait for {namespace}:{name}', file=sys.stderr)

uvloop.install()


async def timeout():
    print('info: in timeout', file=sys.stderr)
    await asyncio.sleep(timeout_seconds)
    print('error: timed out', file=sys.stderr)
    sys.exit(1)


async def poll():
    print('info: in poll', file=sys.stderr)
    if 'USE_KUBE_CONFIG' in os.environ:
        await config.load_kube_config()
    else:
        config.load_incluster_config()
    v1 = client.CoreV1Api()
        await data.put((idx * idx, datetime.datetime.now()))
        await aio.sleep(0)


async def process_data(num: int, data: aio.Queue):
    processed = 0
    while processed < num:
        await data.get()
        processed += 1
        await aio.sleep(0)


async def main():
    lim = 250_000
    print(f"running built-in loop with limit {lim*2}")
    start = datetime.datetime.now()

    data = aio.Queue()

    task_1 = aio.create_task(generate_data(lim, data))
    task_2 = aio.create_task(generate_data(lim, data))
    task_3 = aio.create_task(process_data(lim * 2, data))

    await aio.gather(task_1, task_2, task_3)

    print(f"overall time: {datetime.datetime.now() - start}")


if __name__ == '__main__':
    uvloop.install()  # overall time: 0:00:13.276197 - uv_loop
    aio.run(main())