From cecd1f3abc9a9fa56e0c1e779b15ccbd7e1753c0 Mon Sep 17 00:00:00 2001 From: Myned Date: Sat, 14 Oct 2017 21:53:55 -0400 Subject: [PATCH] Changed session pool to be active only when needed (issues arose with a globally active session) --- src/main/utils/utils.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/main/utils/utils.py b/src/main/utils/utils.py index 45806af..7fcc9f8 100644 --- a/src/main/utils/utils.py +++ b/src/main/utils/utils.py @@ -2,31 +2,39 @@ import asyncio import json import os import pickle as pkl +import subprocess -import aiohttp as aio +import aiohttp +from pync import Notifier + +print('\nPID : {}\n'.format(os.getpid())) + + +# def notify(message): +# subprocess.run(['terminal-notifier', '-message', message, '-title', +# 'Modumind', '-activate', 'com.apple.Terminal', '-appIcon', 'icon.png', '-sound', 'Ping'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) -print('PID {}'.format(os.getpid())) try: with open('config.json') as infile: config = json.load(infile) - print('\"config.json\" loaded.') + print('config.json loaded.') except FileNotFoundError: with open('config.json', 'w') as outfile: json.dump({'client_id': 0, 'listed_ids': [0], 'owner_id': 0, 'permissions': 126016, 'prefix': ',', 'shutdown_channel': 0, 'startup_channel': 0, 'token': 'str'}, outfile, indent=4, sort_keys=True) raise FileNotFoundError( - 'Config file not found: \"config.json\" created with abstract values. Restart \"run.py\" with correct values.') + 'Config file not found: config.json created with abstract values. Restart run.py with correct values.') def setdefault(filename, default=None): try: with open(filename, 'rb') as infile: - print('\"{}\" loaded.'.format(filename)) + print('{} loaded.'.format(filename)) return pkl.load(infile) except FileNotFoundError: with open(filename, 'wb+') as iofile: - print('File not found: \"{}\" created and loaded with default values.'.format(filename)) + print('File not found: {} created and loaded with default values.'.format(filename)) pkl.dump(default, iofile) iofile.seek(0) return pkl.load(iofile) @@ -61,14 +69,9 @@ async def clear(obj, interval=10 * 60, replace=None): asyncio.sleep(interval) -session = None - -HEADERS = {'user-agent': 'Modumind/0.0.1 (Myned)'} - - async def fetch(url, *, params={}, json=False): - global session, HEADERS - async with session.get(url, params=params, headers=HEADERS) as r: - if json is True: - return await r.json() - return r + async with aiohttp.ClientSession() as session: + async with session.get(url, params=params, headers={'user-agent': 'Modumind/0.0.1 (Myned)'}) as r: + if json is True: + return await r.json() + return r