def launch_start(data): from launch import run if PY2: data = { k: v.encode('utf-8') if isinstance(v, unicode) else v for k, v in data.items() } prof_r, prof_w = os.pipe() try: child = os.fork() if not child: # we are the child try: os.setpgrp() # this pgrp is killed if the job fails os.close(prof_r) status_fd = int(os.getenv('BD_STATUS_FD')) keep = [prof_w, status_fd] dispatch.close_fds(keep) data['prof_fd'] = prof_w run(**data) finally: os._exit(0) except Exception: os.close(prof_r) raise finally: os.close(prof_w) return child, prof_r
def mainLoop(lstWater, ground, bucket=None, sponge=[], block=[], collect=[]): pygame.init() pygame.display.set_caption('Water Simulation') pygame.font.init() myfont = pygame.font.SysFont('Monaco', 30) display = (600, 600) screen = pygame.display.set_mode(display) sim = System() sim.particles = copy.deepcopy(lstWater) sim.wall = copy.deepcopy(ground) sim.blockLst = copy.deepcopy(block) sim.dubLst = copy.deepcopy(collect) sim.bucketLoc = copy.deepcopy(bucket) sim.spongeLst = copy.deepcopy(sponge) sim.winningAmount = int(len(sim.particles) // 2) pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sim.particles = copy.deepcopy(lstWater) sim.wall = copy.deepcopy(ground) sim.blockLst = copy.deepcopy(block) sim.dubLst = copy.deepcopy(collect) sim.bucketLoc = copy.deepcopy(bucket) sim.spongeLst = copy.deepcopy(sponge) # sim.collectHit1 = 0 # sim.collectHit2 = 0 # sim.collectHit3 = 0 sim.amountInBucket = 0 launch.run() quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_h: sim.paused = not sim.paused if event.key == pygame.K_r: sim.particles = copy.deepcopy(lstWater) sim.wall = copy.deepcopy(ground) sim.blockLst = copy.deepcopy(block) sim.dubLst = copy.deepcopy(collect) sim.bucketLoc = copy.deepcopy(bucket) sim.spongeLst = copy.deepcopy(sponge) # sim.collectHit1 = 0 # sim.collectHit2 = 0 # sim.collectHit3 = 0 sim.amountInBucket = 0 if pygame.mouse.get_pressed()[0] and sim.removable > 0: pos = pygame.mouse.get_pos() for wall in sim.wall: if wall.collidepoint(pos): sim.wall.remove(wall) sim.removable -= 1 update(sim, display) render(screen, sim) pygame.display.flip()
def launch_start(data): from launch import run if PY2: data = { k: v.encode('utf-8') if isinstance(v, unicode) else v for k, v in data.items() } prof_r, prof_w = os.pipe() # Disable the GC here, leaving it disabled in the child (the method). # The idea is that most methods do not actually benefit from the GC, but # they may well be significantly slowed by it. # Additionally, as seen in https://bugs.python.org/issue31558 # the GC sometimes causes considerable extra COW after fork. # (If prepare_res is something GC-tracked.) # Once gc.freeze is available we will probably want to call that before # splitting the analysis processes if the method has re-enabled gc. try: gc.disable() child = os.fork() if not child: # we are the child try: os.setpgrp() # this pgrp is killed if the job fails os.close(prof_r) status_fd = int(os.getenv('BD_STATUS_FD')) keep = [prof_w, status_fd] dispatch.close_fds(keep) data['prof_fd'] = prof_w run(**data) finally: os._exit(0) except Exception: os.close(prof_r) raise finally: os.close(prof_w) gc.enable() return child, prof_r
def new_runners(config): from dispatch import run if 'py' in runners: del runners['py'] for runner in itervalues(runners): runner.kill() runners.clear() py_v = 'py3' if PY3 else 'py2' todo = {py_v: sys.executable} for k, v in iteritems(config): if re.match(r"py\d+$", k): todo[k] = v for k, py_exe in iteritems(todo): sock_p, sock_c = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM) cmd = [py_exe, './runner.py', str(sock_c.fileno())] pid = run(cmd, [sock_p.fileno()], [sock_c.fileno()], False) sock_c.close() runners[k] = Runner(pid=pid, sock=sock_p) runners['py'] = runners[py_v] return runners
def mainLoop(): #pygame adapted from pygame documentation and tutorial when making 3d water #http://pyopengl.sourceforge.net #https://pythonprogramming.net/opengl-rotating-cube-example-pyopengl-tutorial/ pygame.init() pygame.display.set_caption('Water Simulation') pygame.font.init() myfont = pygame.font.SysFont('Monaco', 30) display = (600, 600) screen = pygame.display.set_mode(display) sim = System() pygame.display.update() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() launch.run() quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_s: if sim.mode == "normal": if sim.placing == "water": sim.placing = "wall" else: sim.placing = "water" else: if sim.placing == "water": sim.placing = "wall" elif sim.placing == "wall": sim.placing = "block" elif sim.placing == "block": sim.placing = "sponge" elif sim.placing == "sponge" and sim.noBuckets: sim.placing = "bucket" elif (sim.placing == "sponge" or sim.placing == "block" \ or sim.placing == "bucket") and len(sim.dubLst) < 3: sim.placing = "dubs" else: sim.placing = "water" if event.key == pygame.K_f: sim.placing = "force" if event.key == pygame.K_h: sim.help = not sim.help if not sim.paused: sim.paused if event.key == pygame.K_e and sim.mode == "builder": if sim.placing != "erase": sim.placing = "erase" else: sim.placing = "water" if event.key == pygame.K_p and sim.mode == "normal": sim.paused = not sim.paused if event.key == pygame.K_m: sim.mode = "builder" if event.key == pygame.K_r: sim.newBoard() if event.key == pygame.K_SPACE and not sim.noBuckets \ and len(sim.dubLst) == 3: pygame.quit() game.mainLoop(sim.particles, sim.wall, sim.bucketLoc, sim.spongeLst, sim.blockLst, sim.dubLst) quit() elif event.type == pygame.MOUSEMOTION and sim.placing == "force": mousePos = pygame.mouse.get_pos() pushParticles(mousePos, sim) if pygame.mouse.get_pressed()[0]: pos = pygame.mouse.get_pos() if sim.placing == "wall": placeItem(sim, pos, sim.wall, sim.particles, sim.blockPic, sim.placing) elif sim.placing == "block": placeItem(sim, pos, sim.wall, sim.particles, sim.block, sim.placing) elif sim.placing == "sponge": placeItem(sim, pos, sim.wall, sim.particles, sim.sponge, sim.placing) elif sim.placing == "water": sim.particles = placeFluid(pos, sim.particles, sim.wall, sim.waterPic) elif sim.placing == "bucket": bucketsCollide(sim, pos) elif sim.placing == "dubs": dubsCollide(sim, pos) elif sim.placing == "erase": if sim.bucketLoc != None and sim.bucketLoc.collidepoint(pos): sim.bucketLoc = None sim.noBuckets = True for wall in sim.wall: if wall.collidepoint(pos): sim.wall.remove(wall) for sponge in sim.spongeLst: if sponge.collidepoint(pos): sim.spongeLst.remove(sponge) for block in sim.blockLst: if block.collidepoint(pos): sim.blockLst.remove(block) for dub in sim.dubLst: if dub.collidepoint(pos): sim.dubLst.remove(dub) update(sim, display) render(screen, sim) pygame.display.flip()