X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=main.py;h=3e9828952867dc7034fb588d865d713655d1a112;hb=b4e0ba0b0aa84550d0b06338b59557c3050798c9;hp=37f485d15c8008aaf8e1fdbc97f99e5f0cd8822a;hpb=a18ce2f16973155c81f983643aba675f62dea7af;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/main.py b/main.py index 37f485d..3e98289 100644 --- a/main.py +++ b/main.py @@ -62,17 +62,18 @@ def make_order(market, value, currency, action="acquire", def get_user_market(config_path, user_id, debug=False): pg_config, report_path = parse_config(config_path) - market_config = list(fetch_markets(pg_config, str(user_id)))[0][0] - return market.Market.from_config(market_config, debug=debug) + market_config = list(fetch_markets(pg_config, str(user_id)))[0][1] + args = type('Args', (object,), { "debug": debug, "quiet": False })() + return market.Market.from_config(market_config, args, user_id=user_id, report_path=report_path) def fetch_markets(pg_config, user): connection = psycopg2.connect(**pg_config) cursor = connection.cursor() if user is None: - cursor.execute("SELECT config,user_id FROM market_configs") + cursor.execute("SELECT id,config,user_id FROM market_configs") else: - cursor.execute("SELECT config,user_id FROM market_configs WHERE user_id = %s", user) + cursor.execute("SELECT id,config,user_id FROM market_configs WHERE user_id = %s", user) for row in cursor: yield row @@ -109,6 +110,9 @@ def parse_args(argv): parser.add_argument("--after", default=False, action='store_const', const=True, help="Run the steps after the cryptoportfolio update") + parser.add_argument("--quiet", + default=False, action='store_const', const=True, + help="Don't print messages") parser.add_argument("--debug", default=False, action='store_const', const=True, help="Run in debug mode") @@ -117,6 +121,8 @@ def parse_args(argv): parser.add_argument("--action", action='append', help="Do a different action than trading (add several times to chain)") + parser.add_argument("--parallel", action='store_true', default=True, dest="parallel") + parser.add_argument("--no-parallel", action='store_false', dest="parallel") args = parser.parse_args(argv) @@ -126,10 +132,12 @@ def parse_args(argv): return args -def process(market_config, user_id, report_path, args): +def process(market_id, market_config, user_id, report_path, args, pg_config): try: market.Market\ - .from_config(market_config, debug=args.debug, user_id=user_id, report_path=report_path)\ + .from_config(market_config, args, + pg_config=pg_config, market_id=market_id, + user_id=user_id, report_path=report_path)\ .process(args.action, before=args.before, after=args.after) except Exception as e: print("{}: {}".format(e.__class__.__name__, e)) @@ -139,8 +147,17 @@ def main(argv): pg_config, report_path = parse_config(args.config) - for market_config, user_id in fetch_markets(pg_config, args.user): - process(market_config, user_id, report_path, args) + if args.parallel: + import threading + market.Portfolio.start_worker() + + for row in fetch_markets(pg_config, args.user): + threading.Thread(target=process, args=[ + *row, report_path, args, pg_config + ]).start() + else: + for row in fetch_markets(pg_config, args.user): + process(*row, report_path, args, pg_config) if __name__ == '__main__': # pragma: no cover main(sys.argv[1:])