def install_reactor(use_asyncio=False): """ Borrowed from https://github.com/crossbario/autobahn-python/blob/master/autobahn/twisted/choosereactor.py """ current_reactor = current_reactor_klass() if current_reactor: return current_reactor if use_asyncio: #files=132, cost=186.99198293685913 seconds, speed=2007542.4309862417 import asyncio import uvloop #asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) asyncio.set_event_loop(uvloop.new_event_loop()) from twisted.internet import asyncioreactor asyncioreactor.install() elif 'bsd' in sys.platform or sys.platform.startswith('darwin'): # This reactor is faster in MacOS # files=132, cost=61.64284586906433 seconds, speed=6089828.182127992 # files=132, cost=58.344452142715454 seconds, speed=6434105.149907892 # *BSD and MacOSX # from twisted.internet import kqreactor kqreactor.install() elif sys.platform in ['win32']: from twisted.internet.iocpreactor import reactor as iocpreactor iocpreactor.install() elif sys.platform.startswith('linux'): from twisted.internet import epollreactor epollreactor.install() else: from twisted.internet import selectreactor selectreactor.install() from twisted.internet import reactor return reactor
async def startup_event(): """ 获取链接 :return: """ # await register_zmq() import asyncio loop = asyncio.get_event_loop() from twisted.internet import asyncioreactor asyncioreactor.install(eventloop=loop) from txrpc.globalobject import GlobalObject with open( os.sep.join([ os.path.dirname(os.path.dirname( os.path.abspath(__file__))), "config.json" ])) as f: GlobalObject().config = json.load(f) from txrpc.client import RPCClient global client client = RPCClient("CLIENT").clientConnect()
def install_reactor(reactor_path, event_loop_path=None): """Installs the :mod:`~twisted.internet.reactor` with the specified import path. Also installs the asyncio event loop with the specified import path if the asyncio reactor is enabled""" reactor_class = load_object(reactor_path) if reactor_class is asyncioreactor.AsyncioSelectorReactor: with suppress(error.ReactorAlreadyInstalledError): if sys.version_info >= (3, 8) and sys.platform == "win32": policy = asyncio.get_event_loop_policy() if not isinstance(policy, asyncio.WindowsSelectorEventLoopPolicy): asyncio.set_event_loop_policy( asyncio.WindowsSelectorEventLoopPolicy()) if event_loop_path is not None: event_loop_class = load_object(event_loop_path) event_loop = event_loop_class() asyncio.set_event_loop(event_loop) else: event_loop = asyncio.get_event_loop() asyncioreactor.install(eventloop=event_loop) else: *module, _ = reactor_path.split(".") installer_path = module + ["install"] installer = load_object(".".join(installer_path)) with suppress(error.ReactorAlreadyInstalledError): installer()
def _install_reactor(): reactor_qual_name = "twisted.internet.reactor" import sys if reactor_qual_name not in sys.modules: if config.core.mode == "proxy": import asyncio from twisted.internet import asyncioreactor loop = asyncio.get_event_loop() asyncioreactor.install(eventloop=loop) elif config.core.mode == "wallet": # TODO, add qmuash support import asyncio from quamash import QEventLoop, QThreadExecutor loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) from twisted.internet import asyncioreactor asyncioreactor.install(eventloop=loop) from twisted.internet import reactor else: import asyncio from quamash import QEventLoop, QThreadExecutor loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) from twisted.internet import asyncioreactor import twisted.internet reactor = asyncioreactor.AsyncioSelectorReactor(loop) twisted.internet.reactor = reactor sys.modules[reactor_qual_name] = reactor return sys.modules.get(reactor_qual_name)
def install_reactor(reactor_path): reactor_class = load_object(reactor_path) if reactor_class is asyncioreactor.AsyncioSelectorReactor: with suppress(error.ReactorAlreadyInstalledError): asyncioreactor.install(asyncio.get_event_loop()) else: *module, _ = reactor_path.split(".") installer_path = module + ["install"] installer = load_object(".".join(installer_path)) with suppress(error.ReactorAlreadyInstalledError): installer()
async def startup_event(): """ 获取链接 :return: """ # await register_zmq() import asyncio loop = asyncio.get_event_loop() from twisted.internet import asyncioreactor asyncioreactor.install(eventloop=loop) from twisted.internet import reactor from txrpc.globalobject import GlobalObject from loguru import logger with open( os.sep.join([ os.path.dirname(os.path.dirname( os.path.abspath(__file__))), "config.json" ])) as f: GlobalObject().config = json.load(f) global server from txrpc.server import RPCServer def fun(): d = RPCServer.callRemote("CLIENT", "client_test") if not d: return None d.addCallback(logger.debug) d.addErrback(logger.error) return d server = RPCServer("SERVER") @server.childConnectHandle def doChildConnect(name, transport): ''' :return ''' logger.debug("{} connected".format(name)) for i in range(1000): reactor.callLater(i * 2 + 1, fun) @server.childLostConnectHandle def doChildLostConnect(childId): ''' :return ''' logger.debug("{} lost connect".format(childId))
def install_reactor(reactor_path): """Installs the :mod:`~twisted.internet.reactor` with the specified import path.""" reactor_class = load_object(reactor_path) if reactor_class is asyncioreactor.AsyncioSelectorReactor: with suppress(error.ReactorAlreadyInstalledError): asyncioreactor.install(asyncio.get_event_loop()) else: *module, _ = reactor_path.split(".") installer_path = module + ["install"] installer = load_object(".".join(installer_path)) with suppress(error.ReactorAlreadyInstalledError): installer()
def __init__(self, listenPort): handler = logging.StreamHandler() formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) log = logging.getLogger('kademlia') log.addHandler(handler) log.setLevel(logging.DEBUG) self.loop = asyncio.get_event_loop() self.loop.set_debug(True) asyncioreactor.install(eventloop=self.loop) self.server = Server() self.server.listen(listenPort)
def _install_asyncio_reactor(): try: import asyncio from twisted.internet import asyncioreactor except ImportError: pass else: # FIXME maybe we don't need this? Adapted from pytest_twisted from twisted.internet.error import ReactorAlreadyInstalledError try: asyncioreactor.install(asyncio.get_event_loop()) except ReactorAlreadyInstalledError: import twisted.internet.reactor if not isinstance(twisted.internet.reactor, asyncioreactor.AsyncioSelectorReactor): raise
def __init__(self, n=None, reactor=None, loop=None, *args, **kwargs): if reactor is None: try: import asyncio loop = loop or asyncio.get_event_loop() try: from twisted.internet import asyncioreactor asyncioreactor.install(loop) except (ReactorAlreadyInstalledError, ImportError): pass except ImportError: pass if n: from twisted.internet import reactor pool = reactor.getThreadPool() pool.adjustPoolsize(0, n) super(AsyncSession, self).__init__(*args, **kwargs)
def _reinstall_reactor(): import sys import asyncio from twisted.internet import asyncioreactor # Uninstall the reactor. if "twisted.internet.reactor" in sys.modules: del sys.modules["twisted.internet.reactor"] # The daphne.server module may have already installed the reactor. # If so, using this module will use uninstalled one, thus we should # reimport this module too. if "daphne.server" in sys.modules: del sys.modules["daphne.server"] event_loop = asyncio.new_event_loop() asyncioreactor.install(event_loop) asyncio.set_event_loop(event_loop)
def install_reactor(reactor_path, event_loop_path=None): """Installs the :mod:`~twisted.internet.reactor` with the specified import path. Also installs the asyncio event loop with the specified import path if the asyncio reactor is enabled""" reactor_class = load_object(reactor_path) if reactor_class is asyncioreactor.AsyncioSelectorReactor: with suppress(error.ReactorAlreadyInstalledError): if event_loop_path is not None: event_loop_class = load_object(event_loop_path) event_loop = event_loop_class() else: event_loop = asyncio.new_event_loop() asyncioreactor.install(eventloop=event_loop) else: *module, _ = reactor_path.split(".") installer_path = module + ["install"] installer = load_object(".".join(installer_path)) with suppress(error.ReactorAlreadyInstalledError): installer()
async def startup_event(): """ 获取链接 :return: """ import asyncio loop = asyncio.get_event_loop() from twisted.internet import asyncioreactor asyncioreactor.install(eventloop=loop) from txfirefly.client import ClientNode from txrpc.globalobject import GlobalObject from twisted.internet import defer with open("config.json", "r") as f: GlobalObject().config = json.load(f) app.state.client = ClientNode("CLIENT") @app.state.client.startServiceHandle def start(): logger.debug("i am start") @app.state.client.startServiceHandle @defer.inlineCallbacks def start2(): logger.debug(32 * "*") ret = yield treq.get("http://httpbin.org") logger.debug(ret) defer.returnValue(ret) @app.state.client.startServiceHandle async def start3(): pass # async with aiohttp.ClientSession() as session: # url = 'http://httpbin.org' # async with session.get(url) as response: # logger.debug(response.status) # logger.debug(await response.text()) app.state.client.install()
def setup_asyncio_reactor(test): import asyncio from twisted.internet import asyncioreactor _old_loop = asyncio.get_event_loop() if 'twisted.internet.reactor' in sys.modules: del sys.modules['twisted.internet.reactor'] loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) asyncioreactor.install(eventloop=loop) def cleanup(): asyncio.get_event_loop().close() asyncio.set_event_loop(_old_loop) test.addCleanup(cleanup) from twisted.internet import reactor return reactor
def _install_reactor(): reactor_qual_name = "twisted.internet.reactor" if reactor_qual_name not in sys.modules: if config.core.mode == "proxy": import asyncio from twisted.internet import asyncioreactor loop = asyncio.get_event_loop() asyncioreactor.install(eventloop=loop) elif config.core.mode == "wallet": # TODO, add qmuash support import asyncio import time from PyQt5.QtWidgets import QApplication, QProgressBar from quamash import QEventLoop, QThreadExecutor app = QApplication(sys.argv) loop = QEventLoop(app) asyncio.set_event_loop(loop) from twisted.internet import asyncioreactor asyncioreactor.install(eventloop=loop) from twisted.internet import reactor return sys.modules.get(reactor_qual_name)
def install() -> None: """Install ``AsyncioSelectorReactor`` as the default Twisted reactor. .. deprecated:: 5.1 This function is provided for backwards compatibility; code that does not require compatibility with older versions of Tornado should use ``twisted.internet.asyncioreactor.install()`` directly. .. versionchanged:: 6.0.3 In Tornado 5.x and before, this function installed a reactor based on the Tornado ``IOLoop``. When that reactor implementation was removed in Tornado 6.0.0, this function was removed as well. It was restored in Tornado 6.0.3 using the ``asyncio`` reactor instead. """ from twisted.internet.asyncioreactor import install install()
def _create_server(self): """ Create a daphne server with local thread asyncio event loop and twisted reactor. """ # Reset reactor to use local thread's event loop from twisted.internet import asyncioreactor del sys.modules["twisted.internet.reactor"] try: event_loop = asyncio.get_event_loop() except RuntimeError: event_loop = asyncio.new_event_loop() asyncioreactor.install(event_loop) from twisted.internet import reactor # Create hook to check if main thread communicated with us reactor.callLater(1, self._on_reactor_hook, reactor) application = self.application if self.static_handler: application = self.static_handler(application) endpoints = build_endpoint_description_strings(host=self.host, port=self.port) def ready(): if self.port == 0: self.port = self.daphne.listening_addresses[0][1] self.is_ready.set() return Server( application=application, endpoints=endpoints, signal_handlers=False, root_path=getattr(settings, "FORCE_SCRIPT_NAME", "") or "", ready_callable=ready, reactor=reactor, )
# ┃ ┗━━━┓ # ┃ ┣┓ # ┃ ┏┛ # ┗┓┓┏━┳┓┏┛ # ┃┫┫ ┃┫┫ # ┗┻┛ ┗┻┛ # 神兽保佑,代码无BUG! # # # import asyncio from twisted.internet import asyncioreactor loop = asyncio.get_event_loop() asyncioreactor.install(eventloop=loop) import json import aiohttp from twisted.internet import defer from txfirefly.client import ClientNode from txfirefly.exts.ffrequest import FFrequest from txrpc.globalobject import GlobalObject from txrpc.utils import asDeferred from loguru import logger with open("config.json","r") as f: GlobalObject().config = json.load(f)
from twisted.internet import asyncioreactor # isort:skip twisted_loop = asyncio.new_event_loop() current_reactor = sys.modules.get("twisted.internet.reactor", None) if current_reactor is not None: if not isinstance(current_reactor, asyncioreactor.AsyncioSelectorReactor): warnings.warn( "Something has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; " + "you can fix this warning by importing daphne.server early in your codebase or " + "finding the package that imports Twisted and importing it later on.", UserWarning, ) del sys.modules["twisted.internet.reactor"] asyncioreactor.install(twisted_loop) else: asyncioreactor.install(twisted_loop) import logging import time from concurrent.futures import CancelledError from functools import partial from autobahn.websocket.compress import PERMESSAGE_COMPRESSION_EXTENSION as EXTENSIONS from twisted.internet import defer, reactor from twisted.internet.endpoints import serverFromString from twisted.logger import STDLibLogObserver, globalLogBeginner from twisted.web import http from .http_protocol import HTTPFactory
# to use the "asyncioreactor" and for code to convert Futures/Tasks to # Deferreds (most of which is already in Deferred) # # Thanks to Mark Williams for the inspiration, and this code: # https://gist.github.com/markrwilliams/bffb9c293194d105169ea06f03484ba1 # # note: if run in Python2, there are SyntaxErrors before we can tell # the user nicely import os import asyncio from twisted.internet import asyncioreactor # get our reactor installed as early as possible, in case other # imports decide to import a reactor and we get the default asyncioreactor.install(asyncio.get_event_loop()) from twisted.internet.task import react from twisted.internet.defer import ensureDeferred, Deferred from twisted.internet.endpoints import UNIXClientEndpoint import txtorcon try: import aiohttp from aiohttp import web from aiosocks.connector import ProxyConnector, ProxyClientRequest except ImportError: raise Exception( "You need aiohttp to run this example:\n pip install aiohttp" )
import datetime import logging from os.path import isfile, join from pprint import pprint from random import choice import celery import celery.bin.base import celery.bin.celery import celery.platforms from celery import Celery from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from twisted.internet import asyncioreactor asyncioreactor.install() # FIXME: from billiard import Process from scrapy.crawler import CrawlerProcess from scrapy.settings import Settings as ScrapySettings from config import ( __short_title__, __title__, WEBUI_DB_URI, FEEDS_DIR, EXTENSIONS, RESULTS_DIR, DEBUG, USER_AGENT, SETTINGS, SETTINGS_FILE,
import logging import os import sys import alot from alot.settings.const import settings from alot.settings.errors import ConfigError from alot.helper import get_xdg_env from alot.db.manager import DBManager from alot.ui import UI from alot.commands import * from alot.commands import CommandParseError, COMMANDS from alot.utils import argparse as cargparse from twisted.internet import asyncioreactor asyncioreactor.install() _SUBCOMMANDS = ['search', 'compose', 'bufferlist', 'taglist', 'namedqueries', 'pyshell'] def parser(): """Parse command line arguments, validate them, and return them.""" parser = argparse.ArgumentParser(add_help=False) parser.add_argument('-r', '--read-only', action='store_true', help='open notmuch database in read-only mode') parser.add_argument('-c', '--config', metavar='FILENAME', action=cargparse.ValidatedStoreAction, validator=cargparse.require_file, help='configuration file')
async def init_task(): loop = asyncio.get_running_loop() conf = Config() pg_client = repository.PgClient(conf["citus_database"], loop) await pg_client.init_connection() return pg_client async def finalyze_task(records, match_repo): await match_repo.insert_many(records) print(f"Seccessfully saved: {len(records)} records") if __name__ == "__main__": loop = asyncio.new_event_loop() asyncioreactor.install(loop) pg_client = loop.run_until_complete(init_task()) match_repo = repository.MatchPgRepository(pg_client) datasets = [ betstady_datasets.epl, betstady_datasets.bundesliga, betstady_datasets.j1_league, betstady_datasets.a_league, betstady_datasets.efl_championship, betstady_datasets.mls, ] process = CrawlerProcess()
def init_asyncio_reactor(): asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) try: asyncioreactor.install() except error.ReactorAlreadyInstalledError: pass
def main(): # We need to install the asyncio reactor before we add any imports like # `twisted.internet.*` which install the default reactor. We keep it here # and not at package level to avoid installing the reactor more than once. # Twisted throws an exception if you install the reactor more than once. import asyncio if sys.platform == 'win32' and sys.version_info >= (3, 7, 0): # we (or twisted) do not support the ProactorEventLoop as it does not # support adding file readers asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) from twisted.internet import asyncioreactor asyncioreactor.install(asyncio.get_event_loop()) from piqueserver.config import (config, TOML_FORMAT, JSON_FORMAT, SUPPORTED_PYTHONS) if (sys.version_info.major, sys.version_info.minor) not in SUPPORTED_PYTHONS: print('Warning: you are running on an unsupported Python version.\n' 'The server may not run correctly.\n' 'Please see https://piqueserver.readthedocs.io/en/v1.0.0/supported-python-environments.html for more information.') description = '%s is an open-source Python server implementation ' \ 'for the voxel-based game "Ace of Spades".' % PKG_NAME arg_parser = argparse.ArgumentParser( prog=PKG_NAME, description=description) if not sys.warnoptions: import warnings warnings.filterwarnings("default", module="piqueserver[.*]") arg_parser.add_argument( '-c', '--config-file', default=None, help='specify the config file - ' 'default is "config.toml" in the config dir') arg_parser.add_argument( '-j', '--json-parameters', help='add extra settings in json format ' '(overrides the config present in the config file)') arg_parser.add_argument( '-d', '--config-dir', default=config.config_dir, help='specify the directory which contains ' 'maps, scripts, etc (in correctly named ' 'subdirs) - default is %s' % config.config_dir) arg_parser.add_argument( '--copy-config', action='store_true', help='copies the default/example config dir to ' 'its default location or as specified by "-d"') arg_parser.add_argument( '--update-geoip', action='store_true', help='download the latest geoip database') arg_parser.add_argument( '--version', action='store_true', help='show the version and exit') args = arg_parser.parse_args() # update the config_dir from cli args config.config_dir = args.config_dir # run the required tasks if args given if args.copy_config or args.update_geoip: if args.copy_config: status = copy_config() if status != 0: sys.exit(status) if args.update_geoip: status = update_geoip(config.config_dir) if status != 0: sys.exit(status) return # if we have done a task, don't run the server if args.version: import piqueserver print("piqueserver", piqueserver.__version__) return # TODO: set config/map/script/log/etc. dirs from config file, thus removing # the need for the --config-dir argument and the config file is then a # single source of configuration # find and load the config # search order: # - --config-file (must have toml or json file extension) # - --config-dir/config.toml # - --config-dir/config.json # - ~/.config/piqueserver/config.toml # - ~/.config/piqueserver/config.json format_ = None if args.config_file is None: for format__, ext in ((TOML_FORMAT, 'toml'), (JSON_FORMAT, 'json')): config_file = os.path.join(config.config_dir, 'config.{}'.format(ext)) format_ = format__ if os.path.exists(config_file): break else: config_file = args.config_file ext = os.path.splitext(config_file)[1] if ext == '.json': format_ = JSON_FORMAT elif ext == '.toml': format_ = TOML_FORMAT else: raise ValueError( 'Unsupported config file format! Must have json or toml extension.' ) config.config_file = config_file print('Loading config from {!r}'.format(config_file)) try: with open(config_file) as fobj: config.load_from_file(fobj, format_=format_) except FileNotFoundError as e: print("Could not open Config file") print(e) return e.errno # update config with cli overrides if args.json_parameters: config.update_from_dict(json.loads(args.json_parameters)) from piqueserver import server server.run()
"""A simple script that attempts to directly download a single blob or stream from a given peer""" import argparse import logging import sys import tempfile import time import shutil from pprint import pprint from twisted.internet import asyncioreactor asyncioreactor.install() from twisted.internet import defer, threads, reactor from lbrynet import conf, log_support from lbrynet.p2p import Peer from lbrynet.p2p.SinglePeerDownloader import SinglePeerDownloader from lbrynet.p2p.StreamDescriptor import BlobStreamDescriptorReader from lbrynet.p2p.BlobManager import DiskBlobManager from lbrynet.extras.daemon.Components import f2d from lbrynet.extras.daemon.storage import SQLiteStorage from lbrynet.extras.wallet import LbryWalletManager log = logging.getLogger() def main(args=None): conf.initialize_settings() parser = argparse.ArgumentParser() parser.add_argument('peer') parser.add_argument('blob_hash') parser.add_argument('--timeout', type=int, default=30)
from scrapy.cmdline import execute from twisted.internet import asyncioreactor import asyncio asyncioreactor.install(asyncio.get_event_loop()) if __name__ == '__main__': execute()
def install_asyncio_reactor(): """ Tries to install AsyncioSelectorReactor """ with suppress(ReactorAlreadyInstalledError): asyncioreactor.install(asyncio.get_event_loop())