예제 #1
0
파일: sharder.py 프로젝트: sgtlaggy/disco
class AutoSharder(object):
    def __init__(self, config):
        self.config = config
        self.client = APIClient(config.token)
        self.shards = {}
        self.config.shard_count = self.client.gateway_bot_get()['shards']
        if self.config.shard_count > 1:
            self.config.shard_count = 10

    def run_on(self, sid, raw):
        func = load_function(raw)
        return self.shards[sid].execute(func).wait(timeout=15)

    def run(self):
        for shard in range(self.config.shard_count):
            if self.config.manhole_enable and shard != 0:
                self.config.manhole_enable = False

            self.start_shard(shard)
            gevent.sleep(6)

        logging.basicConfig(
            level=logging.INFO,
            format=
            '{} [%(levelname)s] %(asctime)s - %(name)s:%(lineno)d - %(message)s'
            .format(id))

    def start_shard(self, sid):
        cpipe, ppipe = gipc.pipe(duplex=True,
                                 encoder=marshal.dumps,
                                 decoder=marshal.loads)
        gipc.start_process(run_shard, (self.config, sid, cpipe))
        self.shards[sid] = GIPCProxy(self, ppipe)
예제 #2
0
class AutoSharder(object):
    def __init__(self, config):
        self.config = config
        self.client = APIClient(config.token)
        self.shards = {}
        self.config.shard_count = self.client.gateway_bot_get()['shards']
        if self.config.shard_count > 1:
            self.config.shard_count = 10

    def run_on(self, sid, raw):
        func = load_function(raw)
        return self.shards[sid].execute(func).wait(timeout=15)

    def run(self):
        for shard in range(self.config.shard_count):
            if self.config.manhole_enable and shard != 0:
                self.config.manhole_enable = False

            self.start_shard(shard)
            gevent.sleep(6)

        logging.basicConfig(
            level=logging.INFO,
            format=
            '{} [%(levelname)s] %(asctime)s - %(name)s:%(lineno)d - %(message)s'
            .format(id))

    @staticmethod
    def dumps(data):
        if isinstance(data, (basestring, int, long, bool, list, set, dict)):
            return '\x01' + marshal.dumps(data)
        elif isinstance(data, object) and data.__class__.__name__ == 'code':
            return '\x01' + marshal.dumps(data)
        else:
            return '\x02' + pickle.dumps(data)

    @staticmethod
    def loads(data):
        enc_type = data[0]
        if enc_type == '\x01':
            return marshal.loads(data[1:])
        elif enc_type == '\x02':
            return pickle.loads(data[1:])

    def start_shard(self, sid):
        cpipe, ppipe = gipc.pipe(duplex=True,
                                 encoder=self.dumps,
                                 decoder=self.loads)
        gipc.start_process(run_shard, (self.config, sid, cpipe))
        self.shards[sid] = GIPCProxy(self, ppipe)