import time import aiohttp URL = 'https://www.xiazaiba.com' async def job(session): response = await session.get(URL) return str(response.url) async def main(_loop): async with aiohttp.ClientSession() as session: tasks = [_loop.create_task(job(session)) for _ in range(5)] finished, unfinished = await asyncio.wait(tasks) all_results = [r.result() for r in finished] # 得到工作回报. print(all_results) if __name__ == '__main__': t2 = time.time() # loop = asyncio.get_event_loop() # Removed selector = selectors.SelectSelector() # New line loop = asyncio.SelectorEventLoop(selector) # New line try: loop.run_until_complete(main(loop)) # 完成事件循环,直到最后一个任务结束 finally: loop.close() # 结束事件循环. print("Async total time:", time.time() - t2)
def get_data_from_api_and_update(student_id, ses, now, last_update, need_to_update_sitelist): """ ユーザーの履修科目を取得し、対象科目の課題、授業資料、テスト・クイズの情報を更新する need_to_update_sitelist: 0 -> すでにデータベースに格納してあるユーザー履修状況をもとに対象科目を定める 1 -> APIで履修科目を取得し、うちデータベースに格納していない科目のみを対象とする """ get_membership = {"student_id": "", "site_list": []} if need_to_update_sitelist == 0: get_membership["student_id"] = student_id get_membership["site_list"] = get_courses_id_to_be_taken(student_id) else: # 時間かかる last_update = 0 # membership.json 使用 # get_membership = get_course_id_from_api(get_membership_json(ses)) # site.json 使用 get_membership = get_course_id_from_site_api(get_site_json(ses), student_id) already_known = get_courses_id_to_be_taken(student_id) # 既存の教科情報を更新 site_list_known = [ i for i in get_membership["site_list"] if i in already_known ] sc_known = [{ "sc_id": f"{student_id}:{i}", "student_id": student_id, "course_id": i } for i in site_list_known] add_studentcourse(student_id, sc_known) # 新規のもののみを取り上げる get_membership["site_list"] = [ i for i in get_membership["site_list"] if i not in already_known ] if student_id != "": # get_assignments = get_assignments_from_api(assignments.json(), student_id) get_sites = {"courses": [], "student_courses": []} get_resources = {"resources": [], "student_resources": []} get_quizzes = {"quizzes": [], "student_quizzes": []} asyncio.set_event_loop(asyncio.SelectorEventLoop()) loop = asyncio.get_event_loop() c_statements = [] s_statements = [] q_statements = [] p_statements = [] for courseid in get_membership["site_list"]: c_statements.append(async_get_content(courseid, ses)) s_statements.append(async_get_site(courseid, ses)) p_statements.append(async_get_site_pages(courseid, ses)) q_statements.append(async_get_quiz(courseid, ses)) # site = s.get(f"{api_url}site/{courseid}.json") # resources = s.get(f"{api_url}content/site/{courseid}.json") # get_site = get_course_from_api(site.json(), student_id) # get_sites["courses"].append(get_site["course"]) # get_sites["student_courses"].append(get_site["student_course"]) # get_resource = get_resources_from_api(resources.json(),courseid,student_id) # get_resources["resources"].append(get_resource["resources"]) # get_resources["student_resources"].append(get_resources["student_resources"]) c_statements.extend(s_statements) c_statements.extend(p_statements) c_statements.extend(q_statements) c_statements.extend( [async_get_assignments(ses), async_get_user_info(ses)]) tasks = asyncio.gather(*c_statements) content_site = loop.run_until_complete(tasks) content_site_len = int(len(content_site)) - 2 one_forrth_content_site_len = content_site_len // 4 contents = content_site[0:one_forrth_content_site_len] sites = content_site[ one_forrth_content_site_len:one_forrth_content_site_len * 2] pages = content_site[one_forrth_content_site_len * 2:one_forrth_content_site_len * 3] quizzes = content_site[one_forrth_content_site_len * 3:content_site_len] get_assignments = get_assignments_from_api( content_site[content_site_len], student_id) user_info = get_user_info_from_api(content_site[content_site_len + 1]) index = 0 for courseid in get_membership["site_list"]: get_resource = get_resources_from_api(contents[index], courseid, student_id) get_quiz = get_quizzes_from_api(quizzes[index], courseid, student_id) get_site = get_course_from_api(sites[index], student_id) if get_site: get_site["course"]["page_id"] = get_page_from_api( pages[index], "assignment") get_site["course"]["quiz_page_id"] = get_page_from_api( pages[index], "quiz") get_sites["courses"].append(get_site["course"]) get_sites["student_courses"].append(get_site["student_course"]) get_resources["resources"].extend(get_resource["resources"]) get_resources["student_resources"].extend( get_resource["student_resources"]) get_quizzes["quizzes"].extend(get_quiz["quizzes"]) get_quizzes["student_quizzes"].extend( get_quiz["student_quizzes"]) index += 1 # student_id student_id # get_membership {"student_id": , "site_list": []} # get_assignments {"assignments": [], student_assignments: []} # get_sites {"courses": [], "student_courses": []} # get_resources {"resources":[], "student_resources": []} # student_quizzes {"quizzes:[], "student_quizzes":[]} # user_info {"student_id": , "fullname": } # + get_quizzes sync_student_contents(student_id, get_sites, get_assignments, get_resources, get_quizzes, now, last_update=last_update, need_to_update_sitelist=need_to_update_sitelist)
def create_event_loop(self): return asyncio.SelectorEventLoop(selectors.SelectSelector())
def getObservations(request): # Dropping records from database Observation.objects.all().delete() localizations = Localization.objects.all().filter( capital__contains="primary") list_of_urls = [] for city in localizations: # Just for test purposes. Data pull from another API (allowing to make more requests per 24h) ''' first = 'http://api.aerisapi.com/observations/closest?p=' lat = city[1] lon = city[2] last = '&client_id=ByruDorHEne2JB64BhP1k&client_secret=Jp4xullRcy6DXTPSTKBGXAvGGTaT04iiUQXPj0ob' url = first + str(lat) + ',' + str(lon) + last list_of_urls.append(url) ''' # https://api.weatherbit.io/v2.0/current?lat=38.9047&lon=-77.0163&key=87440aed6e82411eb182a15f12d9d645 first = 'https://api.weatherbit.io/v2.0/current?lat=' lat = city.latitude connector = '&lon=' lon = city.longitude last = '&key=' # Set the token in admin dashboard: 'http://localhost:8000/admin/wind_api/apitoken/' # Free user is allowet to make only 1000 requests per 24h. Unfortunately you need to get new token after every 4 data updates. token = APItoken.objects.get(id=1).token url = first + str(lat) + connector + str(lon) + last + token list_of_urls.append(url) list = [] # Asynchronyous data pull async def fetch(session, url): async with session.get(url) as response: # list.append(response.content.read(1024)) async for line in response.content: list.append(line.decode('utf-8')) return await response.release() async def main(loop): urls = list_of_urls async with aiohttp.ClientSession(loop=loop) as session: resp = [fetch(session, url) for url in urls] await asyncio.gather(*resp) # loop = asyncio.get_event_loop() loop = asyncio.SelectorEventLoop() asyncio.set_event_loop(loop) loop.run_until_complete(main(loop)) loop.close() bulk_list = [] # Creating objects from data pulled from API weatherbit.io for i in list: i = json.loads(i) print(i) observation_time = i['data'][0]['ob_time'] country_code = i['data'][0]['country_code'] city_name = i['data'][0]['city_name'] latitude = i['data'][0]['lat'] longitude = i['data'][0]['lon'] wind_speed = i['data'][0]['wind_spd'] wind_direction_full = i['data'][0]['wind_cdir_full'] wind_direction_short = i['data'][0]['wind_cdir'] wind_direction_degrees = i['data'][0]['wind_dir'] bulk_object = Observation( observation_time=observation_time, country_code=country_code, city_name=city_name, latitude=latitude, longitude=longitude, wind_speed=wind_speed, wind_direction_full=wind_direction_full, wind_direction_short=wind_direction_short, wind_direction_degrees=wind_direction_degrees) bulk_list.append(bulk_object) # Bulk insert to database Observation.objects.bulk_create(bulk_list) return HttpResponse( '<p>Fresh data is already in the database. Feel free to play with API</p>' )
def init_windows(): if platform.system() == 'Windows': asyncio.set_event_loop(asyncio.SelectorEventLoop())
def run(self, cmd, protocol=None, stdin=None, cwd=None, env=None, **kwargs): """Execute a command and communicate with it. Parameters ---------- cmd : list or str Sequence of program arguments. Passing a single string causes execution via the platform shell. protocol : WitlessProtocol, optional Protocol class handling interaction with the running process (e.g. output capture). A number of pre-crafted classes are provided (e.g `KillOutput`, `NoCapture`, `GitProgress`). stdin : byte stream, optional File descriptor like, used as stdin for the process. Passed verbatim to subprocess.Popen(). cwd : path-like, optional If given, commands are executed with this path as PWD, the PWD of the parent process is used otherwise. Overrides any `cwd` given to the constructor. env : dict, optional Environment to be used for command execution. If `cwd` was given, 'PWD' in the environment is set to its value. This must be a complete environment definition, no values from the current environment will be inherited. Overrides any `env` given to the constructor. kwargs : Passed to the Protocol class constructor. Returns ------- dict At minimum there will be keys 'stdout', 'stderr' with unicode strings of the cumulative standard output and error of the process as values. Raises ------ CommandError On execution failure (non-zero exit code) this exception is raised which provides the command (cmd), stdout, stderr, exit code (status), and a message identifying the failed command, as properties. FileNotFoundError When a given executable does not exist. """ if protocol is None: # by default let all subprocess stream pass through protocol = NoCapture cwd = cwd or self.cwd env = self._get_adjusted_env( env or self.env, cwd=cwd, ) # start a new event loop, which we will close again further down # if this is not done events like this will occur # BlockingIOError: [Errno 11] Resource temporarily unavailable # Exception ignored when trying to write to the signal wakeup fd: # It is unclear to me why it happens when reusing an event looped # that it stopped from time to time, but starting fresh and doing # a full termination seems to address the issue if sys.platform == "win32": # use special event loop that supports subprocesses on windows event_loop = asyncio.ProactorEventLoop() else: event_loop = asyncio.SelectorEventLoop() asyncio.set_event_loop(event_loop) try: # include the subprocess manager in the asyncio event loop results = event_loop.run_until_complete( run_async_cmd( event_loop, cmd, protocol, stdin, protocol_kwargs=kwargs, cwd=cwd, env=env, )) finally: # be kind to callers and leave asyncio as we found it asyncio.set_event_loop(None) # terminate the event loop, cannot be undone, hence we start a fresh # one each time (see BlockingIOError notes above) event_loop.close() # log before any exception is raised lgr.log(8, "Finished running %r with status %s", cmd, results['code']) # make it such that we always blow if a protocol did not report # a return code at all if results.get('code', True) not in [0, None]: # the runner has a better idea, doc string warns Protocol # implementations not to return these results.pop('cmd', None) results.pop('cwd', None) raise CommandError( # whatever the results were, we carry them forward cmd=cmd, cwd=self.cwd, **results, ) # denoise, must be zero at this point results.pop('code', None) return results
from multiprocessing import freeze_support from prompt_toolkit import PromptSession, HTML from prompt_toolkit.completion import WordCompleter, NestedCompleter from distutils.version import StrictVersion from CB import HEADERS, HEADLESS_TERMINAL_THEME, __version__ from CB.Core import Core from CB.Compat import pause, timeout, clear, set_terminal_title, set_terminal_size, getch, kbhit from CB.Wago import WagoUpdater if platform.system() == 'Windows': from ctypes import windll, wintypes # FIXME - Python bug #39010 - Fixed in 3.8.6/3.9 import asyncio import selectors asyncio.set_event_loop(asyncio.SelectorEventLoop(selectors.SelectSelector())) class TUI: def __init__(self): self.core = Core() self.session = PromptSession(reserve_space_for_menu=6, complete_in_thread=True) self.headless = False self.console = None self.table = None self.cfSlugs = None self.wowiSlugs = None self.completer = None self.os = platform.system() install()
DISK = sensor.get_all_drives( ) # Disk List z.B. {"speicher": {"C:\\": {"total": ["475.50", "GB", "100.0"], "used": [ # "409.64", "GB", "86.1"], "free": ["65.86", "GB", "13.9"], "enabled": true, "warning": 85, "critical": 95}, # "D:\\": {"total": ["232.89", "GB", "100.0"], "used": ["116.42", "GB", "50.0"], "free": ["116.47", "GB", "50.0"], # "enabled": true, "warning": 85, "critical": 95}, "E:\\": {"enabled": false, "warning": 85, "critical": 95}, # "F:\\": {"total": ["232.85", "GB", "100.0"], "used": ["148.97", "MB", "0.1"], "free": ["232.70", "GB", "99.9"], # "enabled": true, "warning": 85, "critical": 95}}} ### TIP: benutze https://www.jsonformatter.io/ um diesen Json Dump leichter zu lesen # loops für die Websockets ## inklusive workaround für windows python 3.8 policy = asyncio.get_event_loop_policy() policy._loop_factory = asyncio.SelectorEventLoop selector = selectors.SelectSelector() clientloop = asyncio.SelectorEventLoop(selector) selector2 = selectors.SelectSelector() serverloop = asyncio.SelectorEventLoop(selector2) def clear(): # Löscht den Inhalt des Terminals system('cls' if name == 'nt' else 'clear') def state_event(): # Gibt eine liste aus allen listen im Json format zurück return json.dumps({ **DISK, **CPU, **MEM, "sleep": sleep, "time": time.strftime('%d.%m.%Y %X')
def run(self): global threadloops if self.name == "CPU": # CPU Thread global CPU while threadloops: try: if not sensor.cfg["CPU"][ "enabled"]: # Wenn der Sensor abgeschaltet ist wird der Thread beended CPU = {"cpu": {"enabled": False}} break CPU = sensor.get_cpu( ) # Setzt die aktuellen werte in die liste i = 0 for cpu in CPU["cpu"]["use"]: i += cpu last = i / len(CPU["cpu"]["use"]) sensor.check_for_log("CPU", round( last, 2)) # überprüft ob die CPU insgesammt über einen der # Alert werte liegt except: if sensor.cfg is None: # wenn die Config nicht richtig geladen ist muss sie gesetzt werden sensor.cfg = sensor.read_config(sensor.config()) time.sleep( sleep - 1 ) # Die CPU braucht 1 Sekunde um die Last zu messen deswegen wird -1 gerechnet elif self.name == "MEM": # RAM Thread global MEM while threadloops: try: if not sensor.cfg["MEM"][ "enabled"]: # Wenn der Sensor abgeschaltet ist wird der Thread beended MEM = {"mem": {"enabled": False}} break MEM = sensor.get_memory( ) # Setzt die aktuellen werte in die liste sensor.check_for_log("MEM", round( MEM["mem"]["used"], 2)) # überprüft ob die auslastung über einen # der Alert werte liegt except: if sensor.cfg is None: sensor.cfg = sensor.read_config(sensor.config()) time.sleep(sleep) elif self.name == "DISK": # Disk Thread global DISK while threadloops: try: DISK = sensor.get_all_drives( ) # Setzt die aktuellen werte in die liste for d in DISK["speicher"]: sensor.check_for_log( d, round(float(DISK["speicher"][d]["used"][2]), 2)) # überprüft ob die # auslastung über einen der Alert werte liegt except: if sensor.cfg is None: sensor.cfg = sensor.read_config(sensor.config()) time.sleep(sleep) elif self.name == "SEND": # Websocket Client Thread ( siehe def send() ) global clientloop time.sleep(3) while threadloops: try: ### Bugfix für windows 3.8 eventloop in thread selector = selectors.SelectSelector() clientloop = asyncio.SelectorEventLoop(selector) asyncio.set_event_loop(clientloop) ### asyncio.get_event_loop().run_until_complete( send(self)) # Startet den Websocket Client except Exception as e: if not "code = 1006" in str(e): sensor.log_event( "WEBSOCKET Client", 9, "Client Killed " + str(e) ) # Schreibt eventuelle errors in die logdatei time.sleep(1) elif self.name == "WEBSOCK": # Websocket Server Thread global serverloop time.sleep(1) while threadloops: try: ### Bugfix für windows 3.8 eventloop in thread selector = selectors.SelectSelector() serverloop = asyncio.SelectorEventLoop(selector) asyncio.set_event_loop(serverloop) ### # schreibt die websocket Server adresse in den Log sensor.log_event( "WEBSOCKET", 9, 'ws://' + sensor.cfg["ws"]["ip"] + ":" + str(sensor.cfg["ws"]["port"])) start_server = websockets.serve(wbs, sensor.cfg["ws"]["ip"], sensor.cfg["ws"]["port"]) asyncio.get_event_loop().run_until_complete( start_server) # Startet den Websocket Server asyncio.get_event_loop().run_forever() except Exception as e: sensor.log_event( "WEBSOCKET", 9, "Server Killed " + str(e)) # Schreibt eventuelle errors in die logdatei if "Errno 10048" in str(e) or "Errno 98" in str( e) or "already in use" in str( e ): ### Error wenn das Tool Zweimal gestartet wird threadloops = False serverloop.stop() clientloop.stop() time.sleep(3) clear() print("Websocket already in use\n\n" + str(e)) input("\nPress Enter to Close") sys.exit(0) time.sleep(1)
def run_main(config: ServerConfig, state: DotAccessDict, trace: bool) -> None: main_thread = threading.current_thread() main_thread.name = "main" main_loop = (asyncio.ProactorEventLoop() if IS_WINDOWS else asyncio.SelectorEventLoop()) asyncio.set_event_loop(main_loop) LOG.info("init dedicated server") terminal = Terminal() try: ds = DedicatedServer( loop=main_loop, exe_path=config.ds.exe_path, config_path=config.ds.get('config_path'), start_script_path=config.ds.get('start_script_path'), wine_bin_path=config.ds.get('wine_bin_path'), stdout_handler=terminal.handle_stdout, stderr_handler=terminal.handle_stderr, prompt_handler=terminal.handle_prompt, ) except Exception: LOG.fatal("failed to init dedicated server", exc_info=True) abort() LOG.info("start dedicated server") ds_start_task = main_loop.create_task(ds.start()) try: main_loop.run_until_complete(ds_start_task) except Exception: ds_start_task.cancel() LOG.fatal("failed to start dedicated server", exc_info=True) abort() LOG.info("wait for dedicated server to boot") try: main_loop.run_until_complete(ds.wait_network_listeners()) except Exception as e: LOG.fatal(e) ds.terminate() main_loop.run_until_complete(ds.wait_finished()) abort() LOG.info("prepare for application start") app_loop = asyncio.SelectorEventLoop() app_start_event = asyncio.Event(loop=main_loop) app_start_event.clear() app_stop_event = asyncio.Event(loop=app_loop) app_stop_event.clear() app_start_ack = functools.partial( main_loop.call_soon_threadsafe, app_start_event.set, ) app_error_ack = functools.partial( main_loop.call_soon_threadsafe, ds.ask_exit, ) app_stop_request = app_stop_event.wait() app_thread = threading.Thread( target=run_app, name="application", kwargs=dict( loop=app_loop, dedicated_server=ds, config=config, state=state, start_ack=app_start_ack, error_ack=app_error_ack, stop_request=app_stop_request, trace=trace, ), daemon=True, ) app_thread.start() app_start_event.wait() LOG.info("set exit handler") exit_handler = functools.partial(main_loop.create_task, ds.ask_exit()) exit_handler = wrap_exit_handler(main_thread, exit_handler) set_exit_handler(main_loop, exit_handler) is_interactive = config.ds.get('is_interactive', True) if is_interactive: LOG.info("start input handler") stdin_handler = make_thread_safe_string_handler(main_loop, ds.input) terminal.listen_stdin(handler=stdin_handler) try: LOG.info("wait for dedicated server to exit") return_code = main_loop.run_until_complete(ds.wait_finished()) except KeyboardInterrupt: pass except Exception: LOG.fatal("failed to wait for dedicated server to exit", exc_info=True) return_code = -1 else: LOG.info(f"dedicated server has exited (return_code={return_code})") finally: app_loop.call_soon_threadsafe(app_stop_event.set) app_thread.join() LOG.info("exit") raise SystemExit(return_code)
def scrape(): e1 = Extractor.from_yaml_file('search_result.yml') e2 = Extractor.from_yaml_file('flip_results.yml') text = request.form.get("search_bar") print(text) url1 = "https://www.amazon.in/s?k={0}&rh=n%3A1375424031&ref=nb_sb_noss".format( text) url2 = "https://www.flipkart.com/search?q={0}&sid=6bo%2Cb5g&as=on&as-show=on&otracker=AS_QueryStore_HistoryAutoSuggest_1_7_na_na_na&otracker1=AS_QueryStore_HistoryAutoSuggest_1_7_na_na_na&as-pos=1&as-type=HISTORY&suggestionId=macbook+pro%7CLaptops&requestId=4b1460e8-fcf5-4369-a655-a2501be025a8&as-backfill=on".format( text) r1 = requests.get(url1, headers=headers1) r2 = requests.get(url2, headers=headers2) sleep(2) data1 = e1.extract(r1.text) data2 = e2.extract(r2.text) product_title1 = [] product_price1 = [] product_img1 = [] product_url1 = [] product_title2 = [] product_price2 = [] product_img2 = [] product_url2 = [] i = 0 for product1 in data1.values(): for item1 in product1: product_title1.append(item1['title']) product_price1.append(item1['price']) product_img1.append(item1['image']) new_url1 = 'https://www.amazon.in' + item1['url'] product_url1.append(new_url1) asyncio.set_event_loop(asyncio.SelectorEventLoop()) data3 = asyncio.get_event_loop().run_until_complete( add_images_urls(data2, url2)) # data3 = asyncio.run(add_images_urls(data2, url2)) # data3 = await add_images_urls(data2, url2) # data3 = loop.run_until_complete(add_images_urls(data2, url2)) # data3 = add_images_urls(data2, url2) for product2 in data3.values(): for item2 in product2: product_title2.append(item2['title']) product_price2.append(item2['price']) product_img2.append(item2['image']) product_url2.append(item2['url']) # new_url2 = 'https://www.flipkart.com' + item2['url'] # product_url2.append(new_url2) # session = HTMLSession() # response = session.get(url2) # response.html.render(sleep=1, scrolldown=20) # # Container for each product being displayed # div = response.html.find('._1UoZlX') # for image in div: # img = image.find('img', first=True) # img_src = img.attrs['src'] # product_img2.append(img_src) return render_template("index2.html", title1=product_title1, price1=product_price1, img1=product_img1, url1=product_url1, title2=product_title2, price2=product_price2, img2=product_img2, url2=product_url2)
elif (var355.type == aiohttp.WSMsgType.CLOSED): pass break yield from function2515() var3322 = argparse.ArgumentParser( description='websocket console client for wssrv.py example.') var3322.add_argument('--host', action='store', dest='host', default='127.0.0.1', help='Host name') var3322.add_argument('--port', action='store', dest='port', default=8080, type=int, help='Port number') if (__name__ == '__main__'): var2601 = var3322.parse_args() if (':' in var2601.host): (var2601.host, var461) = var2601.host.split(':', 1) var2601.port = int(var461) var2042 = 'http://{}:{}'.format(var2601.host, var2601.var461) var3984 = asyncio.SelectorEventLoop(selectors.SelectSelector()) asyncio.set_event_loop(var3984) var3984.add_signal_handler(signal.SIGINT, var3984.stop) asyncio.Task(function2101(var3984, var2042)) var3984.run_forever()
def get_loop() -> asyncio.AbstractEventLoop: asyncio.set_event_loop(asyncio.SelectorEventLoop()) return asyncio.get_event_loop()
def _configure_event_loop(self): if sys.platform == 'win32': asyncio.set_event_loop(asyncio.ProactorEventLoop()) else: asyncio.set_event_loop(asyncio.SelectorEventLoop())
def event_loop(): loop = asyncio.SelectorEventLoop() yield loop loop.close()
def event_loop(): # We use a SelectorEventLoop on all platforms so our test suite should use a similar event loop. return asyncio.SelectorEventLoop()
def event_loop_selector(): loop1 = asyncio.ProactorEventLoop() loop2 = asyncio.SelectorEventLoop()
def coroInThread(_loop, coro, *args): app = QApplication.instance() loop = _loop if _loop else asyncio.SelectorEventLoop() return loop.run_until_complete(coro(app, loop, *args))
ARGS = argparse.ArgumentParser( description="websocket console client for wssrv.py example.") ARGS.add_argument('--host', action="store", dest='host', default='127.0.0.1', help='Host name') ARGS.add_argument('--port', action="store", dest='port', default=8080, type=int, help='Port number') if __name__ == '__main__': args = ARGS.parse_args() if ':' in args.host: args.host, port = args.host.split(':', 1) args.port = int(port) url = 'http://{}:{}'.format(args.host, args.port) loop = asyncio.SelectorEventLoop(selectors.SelectSelector()) asyncio.set_event_loop(loop) loop.add_signal_handler(signal.SIGINT, loop.stop) asyncio.Task(start_client(loop, url)) loop.run_forever()
def init(self): self.loop = asyncio.SelectorEventLoop() self.thread = Thread(target=self.loop.run_forever) self.thread.start() self.schedule_next()
import asyncio from pprint import pprint from pyre.server import Server try: import uvloop uvloop.install() except ImportError: loop = asyncio.SelectorEventLoop() asyncio.set_event_loop(loop) async def suprise( scope, send, receive, ): # print(pprint(scope)) try: body = receive() # print(body) except BlockingIOError: fut = asyncio.get_event_loop().create_future() receive.subscribe(fut.set_result) # print("waiting") # print(await fut) send.send_start(200, ( (b"Content-Length", b"13"), (b"Content-Type", b"text/plain"),
def setUp(self): self.loop = asyncio.SelectorEventLoop()
def test(self): """ Play PyChess-PyChess 1 min variant games """ if sys.platform == "win32": from asyncio.windows_events import ProactorEventLoop loop = ProactorEventLoop() asyncio.set_event_loop(loop) else: loop = asyncio.SelectorEventLoop() asyncio.set_event_loop(loop) loop = asyncio.get_event_loop() loop.set_debug(enabled=True) for vari in PYCHESS_VARIANTS: variant = variants[vari] def coro(): self.p0 = yield from discoverer.initEngine( self.engine, WHITE, False) self.p1 = yield from discoverer.initEngine( self.engine, BLACK, False) loop.run_until_complete(coro()) def optionsCallback(engine): engine.setOptionVariant(variant) engine.setOptionStrength(1, False) engine.setOptionTime(60, 0, 0) self.p0.connect("readyForOptions", optionsCallback) self.p1.connect("readyForOptions", optionsCallback) def coro(variant): self.game = GameModel(TimeModel(60, 0), variant) self.game.setPlayers([self.p0, self.p1]) def on_game_end(game, state, event): event.set() event = asyncio.Event() self.game.connect("game_ended", on_game_end, event) self.p0.prestart() self.p1.prestart() if self.game.variant.need_initial_board: for player in self.game.players: player.setOptionInitialBoard(self.game) print(variant.name) self.game.start() yield from event.wait() pgn = StringIO() print(save(pgn, self.game)) self.assertIsNone(self.p0.invalid_move) self.assertIsNone(self.p1.invalid_move) loop.run_until_complete(coro(variant))
def main(timer: func.TimerRequest): loop = asyncio.SelectorEventLoop() asyncio.set_event_loop(loop) loop.run_until_complete(try_log()) loop.close()
def event_loop(): """Create an instance of the default event loop for each test case.""" loop = asyncio.SelectorEventLoop() yield loop loop.close()
"customers": CUSTOMERS, "movements": MOVEMENTS, "transactions": TRANSACTIONS } port = int(os.getenv('PORT', 8080)) redis_host = os.environ['REDIS_SERVER'] redis_port = int(os.environ['REDIS_PORT']) redis_password = os.environ['REDIS_PASSWORD'] redis_poolsize = int(os.environ['REDIS_POOL']) app = Quart(__name__) if not hasattr(sys, 'loop'): executor = concurrent.futures.ThreadPoolExecutor() selector = selectors.SelectSelector() sys.loop = asyncio.SelectorEventLoop(selector) sys.loop.set_default_executor(executor) asyncio.set_event_loop(sys.loop) conn = None def add_months(source_date, months): month = source_date.month - 1 + months year = source_date.year + month // 12 month = month % 12 + 1 day = min(source_date.day, calendar.monthrange(year, month)[1]) return datetime(year, month, day) async def serialize(data): global lists
import asyncpg import asyncio import aiohttp import logging import traceback from discord.ext import commands from db import emotes from utils.caches import cache, CacheManager from logging.handlers import RotatingFileHandler # this section is for the new gateway (latest discord.py version) intents = discord.Intents.default() intents.members = True intents.presences = True asyncio.set_event_loop(asyncio.SelectorEventLoop()) async def run(): description = "A bot written in Python that uses asyncpg to connect to a postgreSQL database." # NOTE: 127.0.0.1 is the loopback address. If your db is running on the same machine as the code, this address will work db = await asyncpg.create_pool(**config.DB_CONN_INFO) bot = Bot(description=description, db=db) if not hasattr(bot, 'uptime'): bot.uptime = datetime.datetime.now() try: await cache(bot) bot.session = aiohttp.ClientSession(loop=bot.loop) await bot.start(config.DISCORD_TOKEN)
def start_tribler_core(base_path, api_port, api_key, root_state_dir, core_test_mode=False): """ This method will start a new Tribler session. Note that there is no direct communication between the GUI process and the core: all communication is performed through the HTTP API. """ logger.info( f'Start tribler core. Base path: "{base_path}". API port: "{api_port}". ' f'API key: "{api_key}". Root state dir: "{root_state_dir}". ' f'Core test mode: "{core_test_mode}"') from tribler_core.check_os import check_and_enable_code_tracing, set_process_priority tribler_core.load_logger_config(root_state_dir) from tribler_core.config.tribler_config import TriblerConfig from tribler_core.modules.process_checker import ProcessChecker from tribler_core.session import Session trace_logger = None # TODO for the moment being, we use the SelectorEventLoop on Windows, since with the ProactorEventLoop, ipv8 # peer discovery becomes unstable. Also see issue #5485. if sys.platform.startswith('win'): asyncio.set_event_loop(asyncio.SelectorEventLoop()) def on_tribler_shutdown(future): future.result() get_event_loop().stop() if trace_logger: trace_logger.close() def shutdown(session, *_): logging.info("Stopping Tribler core") ensure_future( session.shutdown()).add_done_callback(on_tribler_shutdown) sys.path.insert(0, base_path) async def start_tribler(): # Check if we are already running a Tribler instance process_checker = ProcessChecker(root_state_dir) if process_checker.already_running: return process_checker.create_lock_file() # Before any upgrade, prepare a separate state directory for the update version so it does not # affect the older version state directory. This allows for safe rollback. version_history = VersionHistory(root_state_dir) version_history.fork_state_directory_if_necessary() version_history.save_if_necessary() state_dir = version_history.code_version.directory config = TriblerConfig(state_dir, config_file=state_dir / CONFIG_FILENAME, reset_config_on_error=True) if not config.get_core_error_reporting_requires_user_consent(): SentryReporter.global_strategy = SentryStrategy.SEND_ALLOWED config.set_api_http_port(int(api_port)) # If the API key is set to an empty string, it will remain disabled if config.get_api_key() not in ('', api_key): config.set_api_key(api_key) config.write( ) # Immediately write the API key so other applications can use it config.set_api_http_enabled(True) priority_order = config.get_cpu_priority_order() set_process_priority(pid=os.getpid(), priority_order=priority_order) global trace_logger # Enable tracer if --trace-debug or --trace-exceptions flag is present in sys.argv trace_logger = check_and_enable_code_tracing('core', config.get_log_dir()) session = Session(config, core_test_mode=core_test_mode) signal.signal(signal.SIGTERM, lambda signum, stack: shutdown(session, signum, stack)) await session.start() logging.getLogger('asyncio').setLevel(logging.WARNING) get_event_loop().create_task(start_tribler()) get_event_loop().run_forever()
def new_loop(self): return asyncio.SelectorEventLoop()
def test_unbuffered_stdio(tmp_path, output_stream, stream_mode, pyi_builder_spec): # Unbuffered text layer was introduced in Python 3.7 if stream_mode == 'text' and not is_py37: pytest.skip("Unbuffered text layer of stdout and stderr streams " "requires Python 3.7 or later.") # Freeze the test program; test_spec() builds the app and runs it, # so explicitly set the number of stars to 0 for this run. pyi_builder_spec.test_spec('pyi_unbuffered_output.spec', app_args=['--num-stars', '0']) # Path to the frozen executable executable = os.path.join(tmp_path, 'dist', 'pyi_unbuffered_output', 'pyi_unbuffered_output') # Expected number of stars EXPECTED_STARS = 5 # Run the test program via asyncio.SubprocessProtocol and monitor # the output class SubprocessDotCounter(asyncio.SubprocessProtocol): def __init__(self, loop, output='stdout'): self.count = 0 self.loop = loop # Select stdout vs stderr assert output in {'stdout', 'stderr'} self.out_fd = 1 if output == 'stdout' else 2 def pipe_data_received(self, fd, data): if fd == self.out_fd: # Treat any data batch that does not end with the * # as irregularity if not data.endswith(b'*'): return self.count += data.count(b'*') def connection_lost(self, exc): self.loop.stop() # end loop.run_forever() # Create event loop if is_win: loop = asyncio.ProactorEventLoop() # for subprocess' pipes on Windows else: loop = asyncio.SelectorEventLoop() asyncio.set_event_loop(loop) counter_proto = SubprocessDotCounter(loop, output=output_stream) # Run try: proc = loop.subprocess_exec(lambda: counter_proto, executable, "--num-stars", str(EXPECTED_STARS), "--output-stream", output_stream, "--stream-mode", stream_mode) loop.run_until_complete(proc) loop.run_forever() finally: loop.close() # Check the number of received stars assert counter_proto.count == EXPECTED_STARS