def cmdrun(self, cmd): comScanCmd = cmd queue = Queue() scanProc = Process( target=self.newProcExecuteCmd, args=[queue, comScanCmd]) scanProc.start() # 等待5秒 scanProc.join(10) try: scanResult = queue.get(timeout=5) except Exception as e: print "get cmd result error" scanResult = -1 scanProc.terminate() return scanResult
def cmdrun(self, cmd): try: comScanCmd = cmd queue = Queue() scanProc = Process(target=self.newProcExecuteCmd, args=[queue, comScanCmd]) scanProc.start() scanProc.join(5) try: scanResult = queue.get(timeout=30) #print scanResult except Exception, e: print e print "get cmd result error: %s " % str(e) scanResult = -1 scanProc.terminate() return scanResult
def run_code(): if not request.args: abort(400) pycode = request.args.get('code', '') if ("__class__" in pycode) or ("_module" in pycode): return jsonify("timed out! you have an infinite loop!") pysplit = pycode.splitlines() # print(pycode, file=sys.stderr) p = Process(target=exec, args=(pycode, myglobals)) p.start() p.join(2) p.terminate() if p.exception: if p.exception == 1: return jsonify("no error!") tb = p.exception[1] if isinstance(p.exception[0], SyntaxError): return getSynTraceback(filename, pysplit, tb) return getTraceback(filename, pysplit, tb) return jsonify("timed out! you have an infinite loop!")
# from urllib2 import urlopen # api_key = 2e714d06ee6612e6e746d6abd9f3b7a9 # weather = urlopen('http://api.openweathermap.org/data/2.5/weather?lat=') # import required functions from AndyPi_LCD import AndyPi_LCD from processing import Process import time import feedparser if __name__ == '__main__': # initial check for latest rss feed msg=feedparser.parse('http://feeds.bbci.co.uk/news/rss.xml?edition=uk').entries[0].title lcd=AndyPi_LCD() # set name of imported class lcd.lcd_init() # initialise LCD lcd.led(512) # turn backlight fully on while True: # setup a new thread process, in which to run the lcd.scroll_clock function, with the correct arguments p = Process(target=lcd.scroll_clock, args=(1, "c", 0.3, msg)) # start the process p.start() # wait for 30 seconds (or however long you wish to wait between checking updates) time.sleep(30.0) # while the python is scrolling the LCD message in the 'p' process # check for new rss feed, and put in variable "msg" msg=feedparser.parse('http://feeds.bbci.co.uk/news/rss.xml?edition=uk').entries[0].title # stop the scroller process p.terminate()
def main(): """Start the Feedback Controller.""" # Get Options description = """Feedback Controller""" usage = "%prog [Options]" version = """ Copyright (C) 2007-2010 Bastian Venthur <venthur at cs tu-berlin de> Homepage: http://bbci.de/pyff This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. """ parser = OptionParser(usage=usage, version=version, description=description) parser.add_option('-l', '--loglevel', type='choice', choices=['critical', 'error', 'warning', 'info', 'debug', 'notset'], dest='loglevel', help='Which loglevel to use for everything but the Feedbacks. Valid loglevels are: critical, error, warning, info, debug and notset. [default: warning]', metavar='LEVEL') parser.add_option('--fb-loglevel', type='choice', choices=['critical', 'error', 'warning', 'info', 'debug', 'notset'], dest='fbloglevel', help='Which loglevel to use for the Feedbacks. Valid loglevels are: critical, error, warning, info, debug and notset. [default: warning]', metavar='LEVEL') parser.add_option('-p', '--plugin', dest='plugin', help="Optional Plugin, the Feedback Controller should inject into the Feedback.", metavar="MODULE") parser.add_option('-a', '--additional-feedback-path', dest='fbpath', help="Additional path to search for Feedbacks.", metavar="DIR") parser.add_option('--port', dest='port', help="Set the Parallel port address to use. Windows only. Should be in Hex (eg: 0x378)", metavar="PORTNUM") parser.add_option("--nogui", action="store_true", default=False, help="Start without GUI.") options, args = parser.parse_args() # Initialize logging str2loglevel = {'critical' : logging.CRITICAL, 'error' : logging.ERROR, 'warning' : logging.WARNING, 'info' : logging.INFO, 'debug' : logging.DEBUG, 'notset' : logging.NOTSET} loglevel = str2loglevel.get(options.loglevel, logging.WARNING) fbloglevel = str2loglevel.get(options.fbloglevel, logging.WARNING) logging.basicConfig(level=loglevel, format='[%(process)-5d:%(threadName)-10s] %(name)-25s: %(levelname)-8s %(message)s') logging.info('Logger initialized with level %s.' % options.loglevel) logging.getLogger("FB").setLevel(fbloglevel) # get the rest plugin = options.plugin fbpath = options.fbpath guiproc = None if not options.nogui: guiproc = Process(target=GUI.main) guiproc.start() port = None if options.port != None: port = int(options.port, 16) try: fc = FeedbackController(plugin, fbpath, port) fc.start() except (KeyboardInterrupt, SystemExit): logging.debug("Caught keyboard interrupt or system exit; quitting") except: logging.error("Caught an exception, quitting FeedbackController.") print traceback.format_exc() finally: print "Stopping FeedbackController...", fc.stop() if guiproc: guiproc.terminate() print "Done."