From 0b102bb07b1af24067178411b58df376256b2452 Mon Sep 17 00:00:00 2001 From: Griatch Date: Thu, 27 Sep 2012 20:51:06 +0200 Subject: [PATCH] Fixed @batchcommand access, automatically disabling procpool under SQLite3. --- ev.py | 1 + src/commands/default/batchprocess.py | 33 +++++++++++++++++++--------- src/utils/utils.py | 29 ++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/ev.py b/ev.py index 8b3bd38664..bb00d8cc8b 100644 --- a/ev.py +++ b/ev.py @@ -236,3 +236,4 @@ class SystemCmds(object): del cmdhandler syscmdkeys = SystemCmds() del SystemCmds + diff --git a/src/commands/default/batchprocess.py b/src/commands/default/batchprocess.py index f86db6df14..01b631b27b 100644 --- a/src/commands/default/batchprocess.py +++ b/src/commands/default/batchprocess.py @@ -65,15 +65,15 @@ for inum in range(len(commands)): print "leaving run ..." """ _PROCPOOL_BATCHCODE_SOURCE = """ -from src.commands.default.batchprocess import batch_cmd_exec, step_pointer, BatchSafeCmdSet -caller.ndb.batch_stack = commands +from src.commands.default.batchprocess import batch_code_exec, step_pointer, BatchSafeCmdSet +caller.ndb.batch_stack = codes caller.ndb.batch_stackptr = 0 -caller.ndb.batch_batchmode = "batch_commands" +caller.ndb.batch_batchmode = "batch_code" caller.cmdset.add(BatchSafeCmdSet) -for inum in range(len(commands)): - print "command:", inum +for inum in range(len(codes)): + print "code:", inum caller.cmdset.add(BatchSafeCmdSet) - if not batch_cmd_exec(caller): + if not batch_code_exec(caller): break step_pointer(caller, 1) print "leaving run ..." @@ -272,7 +272,14 @@ class CmdBatchCommands(MuxCommand): else: caller.msg("Running Batch-command processor - Automatic mode for %s (this might take some time) ..." % python_path) - if False:#TODO - need to add a procpool solution. settings.PROCPOOL_ENABLED: + procpool = False + if "PythonProcPool" in utils.server_services(): + if utils.uses_database("sqlite3"): + caller.msg("Batchprocessor disabled ProcPool under SQLite3.") + else: + procpool=True + + if procpool: # run in parallel process def callback(r): caller.msg(" {GBatchfile '%s' applied." % python_path) @@ -366,7 +373,13 @@ class CmdBatchCode(MuxCommand): else: caller.msg("Running Batch-code processor - Automatic mode for %s ..." % python_path) - if False: #TODO Add procpool solution. settings.PROCPOOL_ENABLED: + procpool = False + if "PythonProcPool" in utils.server_services(): + if utils.uses_database("sqlite3"): + caller.msg("Batchprocessor disabled ProcPool under SQLite3.") + else: + procpool=True + if procpool: # run in parallel process def callback(r): caller.msg(" {GBatchfile '%s' applied." % python_path) @@ -374,10 +387,10 @@ class CmdBatchCode(MuxCommand): def errback(e): caller.msg(" {RError from processor: '%s'" % e) purge_processor(caller) - utils.run_async(_PROCPOOL_BATCHCODE_SOURCE, commands=commands, caller=caller, at_return=callback, at_err=errback) + utils.run_async(_PROCPOOL_BATCHCODE_SOURCE, codes=codes, caller=caller, at_return=callback, at_err=errback) else: # un in-process (will block) - for inum in range(len(commands)): + for inum in range(len(codes)): # loop through the batch file if not batch_cmd_exec(caller): return diff --git a/src/utils/utils.py b/src/utils/utils.py index a09f39fac0..8c53873a6a 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -461,6 +461,35 @@ def format_table(table, extra_space=1): for icol, col in enumerate(table)]) return ftable +def server_services(): + """ + Lists all services active on the Server. Observe that + since services are launced in memory, this function will + only return any results if called from inside the game. + """ + from src.server.sessionhandler import SESSIONS + if hasattr(SESSIONS, "server") and hasattr(SESSIONS.server, "services"): + server = SESSIONS.server.services.namedServices + else: + print "This function must be called from inside the evennia process." + server = {} + del SESSIONS + return server + +def uses_database(name="sqlite3"): + """ + Checks if the game is currently using a given database. This is a + shortcut to having to use the full backend name + + name - one of 'sqlite3', 'mysql', 'postgresql_psycopg2' or 'oracle' + """ + try: + engine = settings.DATABASES["default"]["ENGINE"] + except KeyError: + engine = settings.DATABASE_ENGINE + return engine == "django.db.backends.%s" % name + + _FROM_MODEL_MAP = None _TO_DBOBJ = lambda o: (hasattr(o, "dbobj") and o.dbobj) or o