#๐Ÿ”’ ADISP or proprietary BackgroundTask wrapper for threading?

8 messages ยท Page 1 of 1 (latest)

spring mulchBOT
#

@slate grotto

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

slate grotto
#

okay so i think some bot deleted my initial post lol

#

ORIGINAL QUESTION

Apparently adisp isn't very well known..., so that's why I'm asking here. I have to use 2.7 and there are no exceptions to this sadly. The threading methodology is proprietary, so I've linked it here. I'll also attach my spaghetti code method that uses the threading methods. It's very rough 'n ready so don't give me too much crap for it. I'm still trying to figure out if I even need to use this threading stuff, because it sounds like adisp and this do kinda the same thing. This is all in the context of a reverse engineered game server I'm working on, so none of what I've written is supposed to be type-safe or blah blah blah whatever; purely to debug and test and all of this is local so I'm not worried about "bad coding practice" at the moment. I'm leaning towards adisp so I don't need to have a callback method everywhere, but if adisp can't "defer" thread blocking tasks like file I/O etc then I would likely have to stick with BackgroundTask (which I already have working fine). Implements:

#
def init():
    global threadManager
    vehicles.init(True, None)
    items.init(True, {})
    if threadManager is None:
        threadManager = BackgroundTask.Manager("DatabaseHandler")
        threadManager.startThreads(15)
        TRACE_MSG('DatabaseHandler :: Initialized background thread manager.')
    return True

def fini():
    global threadManager
    if threadManager is not None:
        threadManager.stopAll()
        threadManager = None
        TRACE_MSG('DatabaseHandler :: Stopped background thread manager.')

def add_task(task):
    if threadManager is None:
        assert threadManager is None, "DatabaseHandler :: Background thread manager is not initialized. Has it been initialized in the personality script?"
        raise RuntimeError("UniversalBackgroundDatabaseHandler is not initialized.")
    threadManager.addBackgroundTask(task)


class GetStatsData(BackgroundTask.BackgroundTask):
    """
    Queries player database for stats data.
    """
    def __init__(self, databaseID, callback):
        self.databaseID = databaseID
        self.callback = callback
        self.result = None
        self.filepath = ResMgr.resolveToAbsolutePath('server/database_files/stats/')
        if not os.path.exists(self.filepath):
            os.makedirs(self.filepath)
    
    def doBackgroundTask(self, bgTaskMgr, threadData):
        TRACE_MSG('GetStatsData (background) :: databaseID=%s' % self.databaseID)
        filename = os.path.join(self.filepath, "%s" % self.databaseID)
        if not os.path.isfile(filename):
            self.result = self.__initEmptyStats()  # dict
            try:
                with open(filename, 'wb') as file:
                    pprint.pprint(self.result, stream=file)
                self.result[('eventsData', '_r')][EVENT_CLIENT_DATA.NOTIFICATIONS] = zlib.compress(
                    cPickle.dumps(self.result[('eventsData', '_r')][EVENT_CLIENT_DATA.NOTIFICATIONS]))
            except Exception as e:
                raise Exception("GetStatsData :: Error occurred while writing stats data (init)=%s" % e)
        else:
            try:
                with open(filename, 'rb') as file:
                    self.result = file.read()  # str
                self.result = eval(self.result)  # dict
                self.result[('eventsData', '_r')][EVENT_CLIENT_DATA.NOTIFICATIONS] = zlib.compress(
                    cPickle.dumps(self.result[('eventsData', '_r')][EVENT_CLIENT_DATA.NOTIFICATIONS]))
            except Exception as e:
                raise Exception("GetStatsData :: Error occurred while fetching stats data=%s" % e)
        bgTaskMgr.addMainThreadTask(self)
    
    def __initEmptyStats(self):
        return initEmptyStats()
    
    def doMainThreadTask(self, bgTaskMgr):
        TRACE_MSG('GetStatsData (foreground) :: databaseID=%s' % self.databaseID)
        self.callback(self.result)

BackgroundTask.py

#

rest of methods n context etc etc

def get_stats(databaseID, callback):
    TRACE_MSG('StatsHandler : get_stats :: databaseID=%s' % databaseID)
    deferred = defer.Deferred()
    deferred.addCallback(callback)
    task = DBHandler.GetStatsData(databaseID, callback)
    DBHandler.add_task(task)
    return deferred

--

@baseRequest(AccountCommands.CMD_ADD_INT_USER_SETTINGS)
def addIntUserSettings(proxy, requestID, settings):
    DEBUG_MSG('AccountCommands.CMD_ADD_INT_USER_SETTINGS :: ', settings)
    
    def callback(rdata):
        bdata = {'rev': requestID, 'prevRev': 0, 'intUserSettings': {}}
        for i in range(0, len(settings), 2):
            k, v = int(settings[i]), int(settings[i + 1])
            rdata[('intUserSettings', '_r')][k] = v
            bdata['intUserSettings'][k] = v
        DEBUG_MSG('addIntUserSettings :: ', bdata)
        proxy.client.update(cPickle.dumps(bdata))
        StatsHandler.update_stats(proxy.databaseID, rdata, TRACE_MSG)
    
    StatsHandler.get_stats(proxy.databaseID, callback)
    
    DEBUG_MSG('addIntUserSettings : set_stats')
    proxy.client.onCmdResponse(requestID, AccountCommands.RES_CACHE, '')
slate grotto
#

okay better, easier question.
does it appear that ADISP could prevent thread-blocking similar to that of BackgroundTask

spring mulchBOT
#

@slate grotto

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.