def simulate(self, worldid): import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) self.bins = {} if worldid == 'any': worldid = scall(self.api.first(module='world'))['id'] print('World %s' % worldid) limit = self.options.get('cycles') cycle = -1 self.bots = {} t0 = time.time() bots_found = False botids = sorted([ t['id'] for t in scall(self.api.find(module='bot', rootid=worldid)) ]) min_cycle = 0 bots_cycle = {bid: 0 for bid in botids} async def run_bot(botid, max_cycles=0): nonlocal min_cycle, bots_cycle from unscbot.models import Bot bot = Bot(botid) for i in range(max_cycles): bots_cycle[botid] = i await asyncio.sleep(0) await bot.select_and_call_action() if min(bots_cycle.values()) > min_cycle: min_cycle = min(bots_cycle.values()) #print(bots_cycle.values()) #print(min_cycle) self.start_new_cycle(min_cycle) self.start_new_cycle(-1) futures = [ run_bot(t['id'], limit) for t in scall(self.api.find(module='bot', rootid=worldid)) ] loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.gather(*futures)) t1 = time.time() for speed in sorted(self.bins.keys()): print('%s, %s' % (speed, self.bins[speed])) print('%s reqs./s.' % int(limit / (t1 - t0) * 11)) if self.options.get('stop'): self.api.stop()
def info(self): worlds = scall(self.api.find(module='world')) if worlds is None: print('ERROR: cannot connect to the API') else: for world in worlds: things = scall(self.api.find(rootid=world['id'])) print('%s, %s, %s' % (world['id'], world['created'], len(things)))
def repop_well(self, worldid): #world.set_dims([10, 10, 10]) # world.save() worldid = self.repop(worldid) for i in range(0, 2): scall( self.api.create(module='well', parentid=worldid, rootid=worldid))
def simulate_old(self, worldid): import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) self.bins = {} if worldid == 'any': worldid = scall(self.api.first(module='world'))['id'] print('World %s' % worldid) limit = self.options.get('cycles') cycle = -1 self.bots = {} t0 = time.time() bots_found = False while True: cycle = self.start_new_cycle(cycle) if limit is not None and cycle >= limit: break pr('Cycle: %s' % cycle) if 1 or not bots_found: #print('HERE') botids = sorted([ t['id'] for t in scall(self.api.find(module='bot', rootid=worldid)) ]) if not botids: break # TODO: remove dead bots from <bots> bots_found = self.run_cycle(botids) # world.end_cycle() # time.sleep(0.1) t1 = time.time() for speed in sorted(self.bins.keys()): print('%s, %s' % (speed, self.bins[speed])) print('%s reqs./s.' % int(limit / (t1 - t0) * 11)) if self.options.get('stop'): self.api.stop()
def stop(self): ret = None try: ret = scall(self.send_request('stop')) except ConnectionClosed: pass return ret
def repop(self, worldid, bot_dims=None): # delete children print('Empty the world') if worldid == 'any': world = scall(self.api.first(module='world')) else: world = scall(self.api.first(id=worldid)) if world is None: print('ERROR: world not found') return worldid = world['id'] for thing in scall(self.api.find(rootid=worldid)): if thing['id'] != worldid: scall(self.api.delete(id=thing['id'])) female = 1 # while True: for i in range(10): try: scall( self.api.create(module='bot', parentid=worldid, female=female, rootid=worldid)) except UnscriptedApiError: break female = 1 - female return worldid
def handle(self, *args, **options): self.options = options self.cargs = options['cargs'] self.api = API_Client() action = options['action'][0] found = 0 if action == 'runserver': self.runserver() found = 1 if action == 'compile': self.compile() found = 1 if action == 'info': self.info() found = 1 if action == 'crunch': self.crunch() found = 1 if action == 'new': scall(self.api.create(module='world')) found = 1 if action == 'reindex': self.reindex() found = 1 if action == 'uncache': from django.core.cache import cache cache.clear() found = 1 if not found: print('ERROR: action not found (%s)' % action) print(self.get_help_string()) print('done')
def run_cycle(self, botids): ret = True futures = [] from unscbot.models import Bot for botid in botids: bot = self.bots.get(botid, None) if bot is None: self.bots[botid] = bot = Bot(botid) bot.initialise() if settings.UNSCRIPTED_REQUEST_ASYNC: futures.append(bot.select_and_call_action()) else: scall(bot.select_and_call_action()) loop = asyncio.get_event_loop() #loop.set_default_executor(ThreadPoolExecutor(1000)) loop.run_until_complete(asyncio.gather(*futures)) return ret
def view_api(request, path): try: loop = asyncio.get_event_loop() except Exception as e: loop = asyncio.set_event_loop(asyncio.new_event_loop()) res = dbutils.scall(api.process(request, path)) # return JsonResponse(res, safe=False, status=api.get_status()) return HttpResponse(dbutils.json.dumps( res), content_type='application/json', status=api.get_status(res))
def conn(self): t0 = time.time() cycles = self.options.get('cycles') o = 0 for i in range(cycles): # print i r = scall(self.api.send_request('', i=i)) # sleep(0.00001) t1 = time.time() print('%s reqs./s.' % int(cycles / (t1 - t0)))
def crunch(self): scall(self.api.delete())
def initialise(self): return scall(self.interact(action='pass'))