9 changed files with 117 additions and 13 deletions
@ -0,0 +1,50 @@ |
|||
import random |
|||
import gipc |
|||
import gevent |
|||
import string |
|||
import weakref |
|||
|
|||
|
|||
def get_random_str(size): |
|||
return ''.join([random.choice(string.ascii_printable) for _ in range(size)]) |
|||
|
|||
|
|||
class GIPCProxy(object): |
|||
def __init__(self, pipe): |
|||
self.pipe = pipe |
|||
self.results = weakref.WeakValueDictionary() |
|||
gevent.spawn(self.read_loop) |
|||
|
|||
def read_loop(self): |
|||
while True: |
|||
nonce, data = self.pipe.get() |
|||
if nonce in self.results: |
|||
self.results[nonce].set(data) |
|||
|
|||
def __getattr__(self, name): |
|||
def wrapper(*args, **kwargs): |
|||
nonce = get_random_str() |
|||
self.results[nonce] = gevent.event.AsyncResult() |
|||
self.pipe.put(nonce, name, args, kwargs) |
|||
return self.results[nonce] |
|||
return wrapper |
|||
|
|||
|
|||
class GIPCObject(object): |
|||
def __init__(self, inst, pipe): |
|||
self.inst = inst |
|||
self.pipe = pipe |
|||
gevent.spawn(self.read_loop) |
|||
|
|||
def read_loop(self): |
|||
while True: |
|||
nonce, func, args, kwargs = self.pipe.get() |
|||
func = getattr(self.inst, func) |
|||
self.pipe.put((nonce, func(*args, **kwargs))) |
|||
|
|||
class IPC(object): |
|||
def __init__(self, sharder): |
|||
self.sharder = sharder |
|||
|
|||
def get_shards(self): |
|||
return {} |
@ -0,0 +1,31 @@ |
|||
import gipc |
|||
|
|||
from disco.client import Client |
|||
from disco.bot import Bot, BotConfig |
|||
from disco.api.client import APIClient |
|||
from disco.gateway.ipc.gipc import GIPCObject, GIPCProxy |
|||
|
|||
|
|||
def run_shard(config, id, pipe): |
|||
config.shard_id = id |
|||
client = Client(config) |
|||
bot = Bot(client, BotConfig(config.bot)) |
|||
GIPCObject(bot, pipe) |
|||
bot.run_forever() |
|||
|
|||
|
|||
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'] |
|||
|
|||
def run(self): |
|||
for shard in range(self.shard_count): |
|||
self.start_shard(shard) |
|||
|
|||
def start_shard(self, id): |
|||
cpipe, ppipe = gipc.pipe(duplex=True) |
|||
gipc.start_process(run_shard, (self.config, id, cpipe)) |
|||
self.shards[id] = GIPCProxy(ppipe) |
Loading…
Reference in new issue