def main(): # Clear the screen (assuming VT100 terminal emulation) print('\x1B[2J') # Configure logging (dual output to stdout and to a log file) logging.basicConfig( level=logging.INFO, format= '%(asctime)s %(processName)s.%(threadName)s %(levelname)s: %(message)s', handlers=[ logging.FileHandler('tims_torrent.log'), logging.StreamHandler(sys.stdout) ]) # Parse the command line arguments - expecting a filename for the torrent file. parser = argparse.ArgumentParser(description='Download a torrent') parser.add_argument("filepath", help="path to the .torrent file") args = parser.parse_args() tfp = args.filepath if os.path.isfile(tfp): logging.info(f"Loading torrent file '{tfp}'\n") else: logging.error(f"File not found: {tfp}", file=sys.stderr) sys.exit(-1) # We have received a tracker filename and confirmed that file exists. Construct a tracker object which will read # the torrent file and parse the metadata. trkr = tracker(tfp) # Tell the tracker object to query the tracker(s) and harvest a list of peers for this torrent if trkr.get_peers(): # PriorityQueue is thread-aware and stores a list of data which it keeps sorted. Instantiate one of those to use # for buffering the received file pieces. Use the index number of each piece as the priority so that the auto- # sorting will cause the queue to give us back the file pieces in order. pq = PriorityQueue() # Kick off the main event loop. This is where we should be able to realize a multi-tasking implementation by # instantiating and launching several event loops to run in parallel as discrete processes. Unfortunately, I # found that some of my classes are not multi-processing safe and I ran out of time to get that working. That # effort involved some substantial changes which had to be reverted prior to submitting this version of the # source code. main_loop = event_loop(1, trkr, pq) main_loop.run() logging.info("Done") else: logging.error("Error: Unable to retrieve any peers") sys.exit(-1)
os.system("chcp 65001") parser = optparse.OptionParser() parser.add_option('-p', '--port', type="int", default=19952, dest="port") parser.add_option('-w', '--workers', type="int", default=50, dest="workers") opt, args = parser.parse_args() server_port = opt.port request_limit = system_config.request_limit workers = opt.workers logger = system_config.logger tornado.log.enable_pretty_logging(logger=logger) interface_manager = InterfaceManager() threading.Thread(target=lambda: event_loop(system_config, model_path, interface_manager)).start() sign.set_auth([{ 'accessKey': system_config.access_key, 'secretKey': system_config.secret_key }]) server_host = "0.0.0.0" logger.info('Running on http://{}:{}/ <Press CTRL + C to quit>'.format( server_host, server_port)) app = make_app(system_config.route_map) http_server = tornado.httpserver.HTTPServer(app) http_server.bind(server_port, server_host) http_server.start(1) # app.listen(server_port, server_host) try:
def draw(): """Displays the maze on the screen""" img = draw_grid(maze, tile_img, tiles) display.blit(img, Rect((0, 0, 384, 224)), Rect((0, 0, 384, 224))) pygame.display.update() def handle_key(key): """Handles key events in the game""" move(maze, DIRECTIONS.get(key)) draw() if __name__ == '__main__': # initialize display pygame.init() pygame.display.set_mode((800, 600)) display = pygame.display.get_surface() tile_img, tiles = load_tiles() # prepare the maze maze = parse_grid(create_maze(12, 7)) maze[1][1] = '*' maze[5][10] = 'x' # start the game draw() event_loop(handle_key)
def draw(): """Displays the maze on the screen""" img = draw_grid(maze, tile_img, tiles) display.blit(img, Rect((0, 0, 384, 224)), Rect((0, 0, 384, 224))) pygame.display.update() def handle_key(key): """Handles key events in the game""" move(maze, DIRECTIONS.get(key)) draw() if __name__ == '__main__': # initialize display pygame.init() pygame.display.set_mode((800, 600)) display = pygame.display.get_surface() tile_img, tiles = load_tiles() # prepare the maze maze = parse_grid(create_maze(12, 7)) maze[1][1] = '*' maze[5][10] = 'x' # start the game draw() event_loop(handle_key)
# .gitignore and requirements.pip could be added to the repo import pygame from pygame import Rect from draw_map import draw_map, level from event_loop import event_loop from load_tiles import load_tiles from moves import move pygame.init() pygame.display.set_mode((640, 400)) display = pygame.display.get_surface() DIRECTIONS = {276: 'LEFT', 275: 'RIGHT', 273: 'UP', 274: 'DOWN'} def game(key): """Handles key events in the game""" move(level, DIRECTIONS.get(key, 0)) map_img = draw_map(level, tile_img, tiles) display.blit(map_img, Rect((0, 0, 224, 224)), Rect((0, 0, 224, 224))) pygame.display.update() if __name__ == '__main__': tile_img, tiles = load_tiles() map_img = draw_map(level, tile_img, tiles) display.blit(map_img, Rect((0, 0, 224, 224)), Rect((0, 0, 224, 224))) pygame.display.update() event_loop(game)