def name(self, value): self._name = value if prctl: if value: prctl.set_name(value) else: prctl.set_name('(no name)')
def parse_args_and_setup_logging(cls): arg_parser = argparse.ArgumentParser(description='FunkyWebServer') arg_parser.add_argument('--process_number', type=int) arg_parser.add_argument('--num_children', type=int) args = arg_parser.parse_args() cls.process_number = args.process_number or 0 logsdir = os.path.join(os.path.dirname(__file__), 'tmp', 'logs') if not os.path.exists(logsdir): os.makedirs(logsdir) logFormatter = logging.Formatter('%(asctime)s %(message)s') loghandler = logging.handlers.TimedRotatingFileHandler(os.path.join(logsdir, "process-%02d-log.txt" % cls.process_number), when="midnight") loghandler.setFormatter(logFormatter) logger = logging.getLogger() logger.addHandler(loghandler) if not sys.platform.startswith('win'): if cls.process_number > 0: prctl.set_name('python-pfchild') else: prctl.set_name('python-pfparent') cls.num_children = args.num_children or 3 if cls._open_file_handle is None and cls.process_number == 0: logging.info("Opening a file and keeping it open") cls._open_file_handle = open(os.path.join(os.path.dirname(__file__), 'tmp', 'testfile.txt'), 'w')
def _process_obj(obj): try: prctl.set_name("event_thread_run") threading.current_thread().name = "event_thread_run" # events received via mqtt transport if Constant.JSON_PUBLISH_SOURCE_HOST in obj: source_host = obj[Constant.JSON_PUBLISH_SOURCE_HOST] elif Constant.JSON_PUBLISH_SRC_HOST in obj: source_host = obj[Constant.JSON_PUBLISH_SRC_HOST] else: L.l.error('Cannot process without mandatory field source_host') return obj[Constant.JSON_PUBLISH_SRC_HOST] = source_host if source_host != Constant.HOST_NAME: if Constant.JSON_PUBLISH_TABLE in obj: table = str(obj[Constant.JSON_PUBLISH_TABLE]) # cls = getattr(sys.modules[tinydb_model.__name__], table) cls = getattr(m, table) # if cls._is_used_in_module: if ('Pwm' in table or 'ZoneCustomRelay' in table or 'Ventilation' in table) and (Constant.HOST_NAME == 'pizero1' or Constant.HOST_NAME == 'netbook'): L.l.info('Got mqtt {}'.format(obj)) cls.save(obj) # else: # L.l.info('Ignoring save for {}'.format(cls.__name__)) # pass else: L.l.error('mqtt message sent from me to me!') except Exception as ex: L.l.error("Error processing event err={}, mqtt={}".format(ex, obj), exc_info=True)
def thread_run(): prctl.set_name("radoneye") threading.current_thread().name = "radoneye" GetRadonValue() prctl.set_name("idle_radoneye") threading.current_thread().name = "idle_radoneye" return 'Processed Radoneye'
def __init__(self, app): threading.Thread.__init__(self) prctl.set_name('Buttons') self.app = app self.lock = threading.Lock() self.reset_state = 'WAIT' self.fan_state = 'WAIT' self.heater_state = 'WAIT' self.dehum_state = 'WAIT' self.fan = False self.heater = False self.dehum = False self.running = True # Setting up GPIOs GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(configuration.reset_lever_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(configuration.fan_lever_pin, GPIO.IN) GPIO.setup(configuration.dehum_lever_pin, GPIO.IN) GPIO.setup(configuration.heater_lever_pin, GPIO.IN)
def run(self) -> None: prctl.set_name(str(self)) if self.in_q is None or self.out_q is None: raise ValueError("in_q or out_q is None") while True: # of type ObjectIdentifierOutput object_identifier_results = self.in_q.get_nowait() if object_identifier_results is None: continue if not isinstance(object_identifier_results, ObjectIdentifierOutput): raise ValueError( "Queue gave values not of type ObjectIdentifierOutput") if object_identifier_results.frame is None: # signal to depency, if any, that the frame was None self.send_results( ObjectIdentifierOutput( objects=None, frame=None, overlay=None, )) self.barrier.wait() continue frame = object_identifier_results.frame object_identifier_objects = object_identifier_results.objects results = self.process_frame(frame, object_identifier_objects) self.send_results(results) self.barrier.wait()
def main(): global ENABLE_DEV_TOOLS ENABLE_DEV_TOOLS = False for arg in sys.argv[1:]: if arg in ('-h', '--help'): usage() sys.exit() elif arg in ('-d', '--dev'): ENABLE_DEV_TOOLS = True else: print "hotot: unrecognized option '%s'" % arg usage() sys.exit(1) try: import i18n except: from gettext import gettext as _ try: import prctl prctl.set_name('hotot') except: pass Gdk.threads_init() config.loads(); agent.init_notify() app = Hotot() agent.app = app Gdk.threads_enter() Gtk.main() Gdk.threads_leave()
def thread_run(): prctl.set_name("atrea") threading.current_thread().name = "atrea" keep_alive() prctl.set_name("idle_atrea") threading.current_thread().name = "idle_atrea" return 'Processed template_run'
def thread_run(): prctl.set_name("presence_wifi") threading.current_thread().name = "presence_wifi" L.l.debug('Processing presence_run') _check_wifi() prctl.set_name("idle") threading.current_thread().name = "idle"
def run(self): """ start the server """ prctl.set_name("flask") threading.current_thread().name = "flask" self.thread = threading.current_thread() while not general_init.P.shutting_down: try: L.l.info('Starting flask web ui on host {} port {}'.format( self._host, self._port)) self._app.run(host=self._host, port=self._port, debug=self._debug, use_reloader=self._use_reloader, threaded=True) except IOError as e: if e.errno == errno.EPIPE: L.l.info('Flask broken pipe, {}'.format(e)) else: L.l.error('IO error init flask on host {} port {}, err={}'. format(self._host, self._port, e), exc_info=1) except Exception as ex: L.l.error('Error init flask on host {} port {}, err={}'.format( self._host, self._port, ex), exc_info=1) time.sleep(1)
def libvirt_worker_loop(loop, start_websockets_server, hypervisors): def _err_handler(self, ctxt, err): print("Error from libvirt : %s", err[2]) print("[{}] entering libvirt_worker_loop".format( threading.get_ident())) prctl.set_name('libvirt_loop') asyncio.set_event_loop(loop) libvirtaio.virEventRegisterAsyncIOImpl() libvirt.registerErrorHandler(_err_handler, '_virt_event') for id_, h in hypervisors.items(): monitor_tasks.append( loop.create_task(h['monitor_instance'].watchdog_loop())) loop.run_until_complete(start_websockets_server) try: loop.run_until_complete(asyncio.gather(*monitor_tasks)) except asyncio.CancelledError: loop.stop() finally: loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()
def thread_run(): prctl.set_name("hvac_vent") threading.current_thread().name = "hvac_vent" adjust() prctl.set_name("idle_hvac_vent") threading.current_thread().name = "idle_hvac_vent" return 'Processed hvac vent'
def thread_run(): prctl.set_name("zwave") threading.current_thread().name = "zwave" # L.l.info("State is {}".format(P.network.state)) try: if not P.initialised: P.initialised = _init_controller() if not P.initialised: P.init_fail_count += 1 if P.init_fail_count > 10: unload() # iterate if inclusion is not started if P.initialised and not P.inclusion_started: for node_id in P.network.nodes: node = P.network.nodes[node_id] if node_id == 2 or not P.thread_run_at_init: if not P.thread_run_at_init: L.l.info("Request state for node {}".format(node)) node.request_state() if not P.thread_run_at_init: P.thread_run_at_init = True sec = (datetime.now() - P.last_value_received).total_seconds() if sec > P.MAX_SILENCE_SEC: L.l.info( "Zwave seems inactive, no value received since {} sec, reset now" .format(sec)) P.initialised = _init_controller() _initial_node_init() except Exception as ex: L.l.error("Error in zwave thread run={}".format(ex), exc_info=True) prctl.set_name("idle_zwave") threading.current_thread().name = "idle_zwave"
def main(): for opt in sys.argv[1:]: if opt in ('-h', '--help'): usage() sys.exit() elif opt in ('-d', '--dev'): config.ENABLE_INSPECTOR = True else: print "hotot: unrecognized option '%s'" % opt usage() sys.exit(1) try: import i18n except: from gettext import gettext as _ try: import prctl prctl.set_name('hotot') except: pass GObject.threads_init() config.loads(); agent.init_notify() app = Hotot() agent.app = app Gdk.threads_enter() Gtk.main() Gdk.threads_leave()
def raspivid_loop(): prctl.set_name("raspivid_loop") logger.info('Starting raspivid loop...') width, height = tuple(resolution.split("x")) proc = subprocess.Popen(["/usr/bin/raspivid", "-rot", str(rotation), "-vf", "-hf", "-t", "0", "-w", width, "-h", height, "-pf", "baseline", "-fps", str(fps), "-b", streaming_bitrate, "-o", "-"], shell = False, stdout = subprocess.PIPE) raspivid = proc.stdout while True: if camera_shutdown: break block = raspivid.read(16384) if not block: break sensor_message = {"n": DEVICE_NAME} sensor_message["vid"] = block sensor_message['ty'] = "camera" binary_packet = msgpack.packb(sensor_message, use_bin_type = True) if websocket_enabled: logger.info("sending camera video block to websocket") try: websocket_queue.put(binary_packet, block = False) except Full: logger.exception("websocket queue full") proc.kill() proc.wait()
def thread_run(): prctl.set_name("") threading.current_thread().name = "" # prctl.set_name("idle") threading.current_thread().name = "idle" return 'Processed template_run'
def run(self) -> None: """ Process target input to yolo from caller will always be a np.ndarray. output is formatted as follows: list((class: str:, score: float, box) Note: box is normalized to [left, top, right, bototm] coordinates """ prctl.set_name(str(self)) if not self.on: logger.info("YOLO is turned off, returning") return self.load_model() while True: # TODO: fix round robin overhead: if a feed is not giving frames, # need to not spend time polling it until frames start coming in # again. How to do this? for in_queue, out_queue in self.round_robin: # request is of type Frame request_frame = in_queue.get_nowait() if request_frame is None: continue if not isinstance(request_frame, Frame): raise ValueError( "Yolo received a frame of not type 'Frame'") if request_frame.frame is None: # Signal to Tracker that a 'None' frame was received out_queue.put(YoloOutput(yolo_objects=None, frame=None)) continue yolo_results = self.get_yolo_results(request_frame) out_queue.put(yolo_results)
def thread_run(): prctl.set_name("rule_run") threading.current_thread().name = "rule_run" process_events() prctl.set_name("idle") threading.current_thread().name = "idle" return 'Processed rules thread_run'
def __init__(self, conn, width, descriptor, name="", scaleFactor=1.1, minNeighbors=5, minSize=(35, 35), log=False, display=False): self.conn = conn self.width = width self.name = name self.scaleFactor = scaleFactor self.minNeighbors = minNeighbors self.minSize = minSize self.log = log self.display = display self.run_loop = True # Set process' name for it to be more easily identifiable with ps prctl.set_name("cdsn_" + name + "Det") # Open the log file for this process if log: logName = name + "Det_stats" self.f = open(logName, "a+") self.bd = BaseDetector(descriptor) time.sleep(0.1)
def update(self): prctl.set_name("E:T Message JSON Sender") while True: # Send update over socket every second time.sleep(1) if self.stopped: log = "message sender has stopped" logging.info(log) print("[", colored("INFO", 'green', attrs=['bold']), " ] " + log) #self.sock0.shutdown(socket.SHUT_RDWR) #self.sock0.detach() if settings.TB_CONNECT: try: self.tb_client.disconnect() log = "disconnecting mqtt clients" logging.info(log) print("[", colored("INFO", 'green', attrs=['bold']), " ] " + log) except Exception as e: print("[", colored("ERROR", 'red', attrs=['bold']), " ] TB disconnect failed: ", str(e)) return if settings.JP_SOCK: self.send_json() if self.tb_telemetry: self.send_telemetry()
def thread_target(): try: logger.debug(f"thread '{name}' starting") if _prctl_available: prctl.set_name(name) if _numa_available and numa.available(): numa.set_localalloc() logger.debug( f"Set NUMA local allocation policy on thread {name}") worker(index) logger.debug(f"thread '{name}' finishing") except Exception: logger.critical("Exception occured in thread; exiting") logger.critical(traceback.format_exc()) # Communicate back the main thread that something bad has happened. # This seems to be the only reliable way to do it. _thread.interrupt_main() # Now we still need to make sure that the main thread doesn't block # on the queue.get/join (as it won't be interrupted). This is an attempt # to make sure that it unblocks. May not be fool-proof though. # # TODO This doesn't really work. We can still block on pushing things # onto the queue. We'll probably have to do something ourselves using # timeouts and stuff to see if an error has occured. if consumer: while True: try: work_queue.task_done() except ValueError: break else: work_queue.put(None)
def thread_run(): prctl.set_name("bms") threading.current_thread().name = "bms" get_status() prctl.set_name("idle_bms") threading.current_thread().name = "idle_bms" return 'Processed bms'
def manage_pages_thread(self): prctl.set_name("thread_pagemanag") while True: is_reload = self.__read_pages() if is_reload: self.__load_pages() sleep(thread_refresh_dynamic_pages)
def thread_run(): prctl.set_name("presence_bt") threading.current_thread().name = "presence_bt" if P.bt_initialised: L.l.debug('Processing presence_run') _check_presence() return 'Processed presence_run'
def run(self): set_name('vrrp_fifo_thread') try: self.create_fifo() except Exception: self.logger.error('FATAL: Unable to create VRRP fifo.', exc_info=True) return log_it = True while True: try: with open(self._vrrp_file) as f: self.logger.info('vrrp fifo connection established') for line in f: if line == self.shutdown_line: return else: self.middleware.call_hook_sync('vrrp.fifo', data=line) except Exception: if log_it: self.logger.warning( 'vrrp fifo connection not established, retrying every %d seconds', self._retry_timeout, exc_info=True ) log_it = False sleep(self._retry_timeout)
def thread_run(): prctl.set_name("youtube") threading.current_thread().name = "youtube" global __file_list_last_change, __uploaded_file_list_date try: found_for_upload = True while found_for_upload: found_for_upload = False for file in __file_list_last_change.keys(): lapsed = (utils.get_base_location_now_date() - __file_list_last_change[file]).total_seconds() if lapsed > 30: if file in __uploaded_file_list_date.keys(): L.l.debug( 'Skip duplicate video upload for file {}'.format( file)) # pass else: upload_file(file) found_for_upload = True if len(__uploaded_file_list_date) > 100: __uploaded_file_list_date.clear() # force upload on fifo principles to maintain movie time order when uploaded break except Exception as ex: L.l.warning('Exception on youtube thread run, err={}'.format(ex)) prctl.set_name("idle_youtube") threading.current_thread().name = "idle_youtube"
def thread_run(): prctl.set_name("plotly") threading.current_thread().name = "plotly" L.l.debug('Processing graph_plotly_run') __upload_cached_plotly_data() __announce_grid_cache() return 'Processed graph_plotly_run'
def __init__(self, port, duration=200., is_active_low=False, start_blinks=0): super(gpioPort, self).__init__() self.terminated = False self.duration = duration self.event = threading.Event() self.port = port self.activate = GPIO.HIGH self.deactivate = GPIO.LOW prctl.set_name('ptrk.GPIO') GPIO.setmode(GPIO.BCM) print("GPIOPort::gpioPort::port: %d" % port) GPIO.setup(self.port, GPIO.OUT) if is_active_low: self.activate = GPIO.LOW self.deactivate = GPIO.HIGH if start_blinks > 0: self.blink(start_blinks) GPIO.output(self.port, self.deactivate) self.daemon = True self.event.clear() self.start()
def threadTimer(function, period=.01, arg=None, alignTime=0.0): nextCall = time.time() startTime = time.time() #print 'startTime', startTime if _platform == "linux" or _platform == "linux2": os.nice(-1) if alignTime: #print 'alignTime', alignTime #print 'startTime', startTime nextCall = alignTime count = 0 while (nextCall - startTime) < 0: nextCall = nextCall + period count += 1 nextCall = nextCall + period * count #print count, nextCall stamp = 0 name = threading.current_thread().getName() + str(function) if _platform == "linux" or _platform == "linux2": try: prctl.set_name(function.__name__) except: pass while 1: stamp += 1 #startTime=time.time() #print name, 'stamp1', stamp, nextCall-1486587172 nextCall = nextCall + period #print name, 'stamp2', stamp, nextCall-1486587172 if arg is not None: function(arg) else: function() #endTime=time.time() #elapse=endTime-startTime count = 0 try: now = time.time() time.sleep(nextCall - now) except Exception as err: #print name, 'threadTimer sleep error is', err, 'for', function #print name, 'stamp3', stamp, nextCall-1486587172, 'nextCall-time.time()', nextCall-now while (nextCall - now) < 0: nextCall = nextCall + period count += 1 #print name, 'stamp4', stamp, 'count', count #print name, 'stamp5', stamp, nextCall-1486587172 nextCall = nextCall + period * count #print name, 'stamp6', stamp, nextCall-1486587172 #nextCall=nextCall+period*(count+1) #print name, 'stamp7', stamp, 'count', count, nextCall-1486587172, nextCall-now try: now = time.time() time.sleep(nextCall - now) except: #print name, 'stamp8', stamp, 'count', count, nextCall-1486587172, nextCall-now #nextCall=nextCall+period*(count+3) time.sleep(period)
def run(): try: prctl.set_name(threading.current_thread().name) logging.getLogger(__name__).info('Starting, #%d', gettid()) f(self, cancel) finally: logging.getLogger(__name__).info('Exiting')
def set_proc_name(new_name): # {{{ try: import prctl prctl.set_proctitle(new_name) prctl.set_name(new_name) except: logging.warning("Unable to set proc name %s:"%new_name, exc_info=True)
def thread_run(): prctl.set_name("rfxcom") threading.current_thread().name = "rfxcom" try: if not P.initialised: _init_board() if P.initialised: L.l.debug('Waiting for RFX event') time_elapsed_minutes = (utils.get_base_location_now_date() - P.last_packet_received).seconds / 60 if time_elapsed_minutes > P.MAX_MINUTES_SILENCE: L.l.warning( 'RFX event not received since {} mins, device err? Reseting!' .format(time_elapsed_minutes)) P.transport.reset() event = P.transport.receive_blocking() __rfx_reading(event) else: if P.init_failed_count > P.MAX_FAILED_RETRY: unload() except IndexError as iex: P.initialised = False P.init_failed_count += 1 utils.sleep(10) except Exception as ex: L.l.error('Error read RFX tty port, err={}'.format(ex), exc_info=True) P.initialised = False P.init_failed_count += 1 utils.sleep(10) prctl.set_name("idle") threading.current_thread().name = "idle"
def thread_run_send(): prctl.set_name("mqtt_send") threading.current_thread().name = "mqtt_send" P.thread_send = threading.current_thread() if mqtt_io.P.client_connected: start_len = len(P.send_json_queue) if start_len > 50: L.l.info('Mqtt SEND len={}'.format(start_len)) # FIXME: complete this, will potentially accumulate too many requests P.mqtt_send_lock.acquire() for [json, topic] in list(P.send_json_queue): res = transport.mqtt_io._send_message(json, topic) if res: P.send_json_queue.remove([json, topic]) else: L.l.info('Failed to send mqtt message, res={}'.format(res)) P.mqtt_send_lock.release() end_len = len(P.send_json_queue) if end_len > 10: L.l.warning( "{} messages are pending for transport, start was {}".format( end_len, start_len)) else: elapsed = (utils.get_base_location_now_date() - mqtt_io.P.last_connect_attempt).total_seconds() if elapsed > 10: L.l.info( "Initialising mqtt as message needs to be sent, elapsed={}". format(elapsed)) mqtt_io.init() prctl.set_name("idle_mqtt_send") threading.current_thread().name = "idle_mqtt_send"
def _run(self): # type: () -> None try: import prctl prctl.set_name(self._name) except ImportError: pass backoff = 0.0 while not self._stop.is_set(): start = time.time() if not self._parent.is_alive(): logger.info('Aborting daemon {}'.format(self._name)) return try: self._tick.clear() self._target() self.sleep(self._get_sleep_interval(start)) backoff = 0.0 except DaemonThreadWait: logger.debug('Waiting {} seconds'.format(self._delay)) self.sleep(self._get_delay()) except Exception as ex: logger.exception('Unexpected error in daemon {}: {}'.format( self._name, ex)) backoff += 1.0 self.sleep(min(5.0, self._get_delay() * backoff)) logger.info('Stopping daemon {}... Done'.format(self._name))
def rxToSocket(): prctl.set_name("rxToSocket") while True: dbg = "" try: if len(msgRX) > 0: message = msgRX.pop() if message is not None: obj = canObj.canObj() obj.readMsg(message) if obj.arbitration()["eventName"] == "rittal status": print(message) if obj.arbitration( )["eventName"] == "rittal status" and obj.arbitration( )["msgType"] == 0x04: #print(obj.arbitration()["eventName"]) #print(obj.arbitration()["msgType"]) adress = obj.handle_power_hub_message() print(adress) plug_nr = 1 for plug_address in adress["plugAddresses"]: if plug_address == 1 or plug_address == 0: plug_id = get_plug_id_by_adress( plug_nr, adress["stripAddress"], adress["nodeAddress"]) strip_id = get_strip_id_by_adress( adress["stripAddress"], adress["nodeAddress"]) if plug_address == 1: status = "on" if plug_id in plugs.keys(): plugs[plug_id].setOn() elif plug_address == 0: status = "off" if plug_id in plugs.keys(): plugs[plug_id].setOff() if plug_id: #print("found: " + plugName + " " + stripName) #print({'leiste': stripName,'plug': plugName,'status': status}) print({ 'leiste': strip_id, 'plug': plug_id, 'status': status }) socketio.emit('plugStatus', { 'plugId': plug_id, 'status': status }, broadcast=True) plug_nr += 1 else: gevent.sleep(0.1) except IndexError as err: # print("indexerror:" + err) pass except Exception as err: print("rxToSocket" + " " + str(err) + " " + str(dbg)) pass
def set_process_title(proc_title): """Sets custom title to current process Requires installed python-prctl module - http://pythonhosted.org/python-prctl/ """ try: import prctl prctl.set_name(proc_title) prctl.set_proctitle(proc_title) return True except (ImportError, AttributeError): return False
def run(self): if prctlimported: prctl.set_name("Tribler"+currentThread().getName()) if self.upnp_type > 0: self.upnp_wrap = UPnPWrapper.getInstance() self.upnp_wrap.register(self.locally_guessed_ext_ip) # Disabled Gertjan's UPnP logging for m24 #from Tribler.Core.Statistics.StatusReporter import get_reporter_instance #reporter = get_reporter_instance() if self.upnp_wrap.test(self.upnp_type): # Disabled Gertjan's UPnP logging for m24 #reporter.add_event("UPnP", "Init'ed") try: shownerror=False # Get external IP address from firewall if self.upnp_type != 1: # Mode 1 doesn't support getting the IP address" ret = self.upnp_wrap.get_ext_ip() if ret == None: shownerror=True self.error_func(self.upnp_type,self.listen_port,0) else: self.got_ext_ip_func(ret) # Do open_port irrespective of whether get_ext_ip() # succeeds, UPnP mode 1 doesn't support get_ext_ip() # get_ext_ip() must be done first to ensure we have the # right IP ASAP. # Open TCP listen port on firewall ret = self.upnp_wrap.open(self.listen_port,iproto='TCP') if ret == False and not shownerror: self.error_func(self.upnp_type,self.listen_port,0) # Open UDP listen port on firewall ret = self.upnp_wrap.open(self.listen_port,iproto='UDP') if ret == False and not shownerror: self.error_func(self.upnp_type,self.listen_port,0,listenproto='UDP') # Disabled Gertjan's UPnP logging for m24 #reporter.add_event("UPnP", "UDP:%d" % ret) except UPnPError,e: self.error_func(self.upnp_type,self.listen_port,1,e) else: # Disabled Gertjan's UPnP logging for m24 #reporter.add_event("UPnP", "Init failed") if self.upnp_type != 3: self.error_func(self.upnp_type,self.listen_port,2) elif DEBUG: print >>sys.stderr,"upnp: thread: Initialization failed, but didn't report error because UPnP mode 3 is now enabled by default"
def run(self): if prctlimported: prctl.set_name("Tribler"+currentThread().getName()) try: try: if DEBUG: print >>sys.stderr,"i2is: Ready to receive remote commands on",self.i2iport self.rawserver.listen_forever(self) except: print_exc() finally: self.rawserver.shutdown()
def main(): # noinspection PyUnresolvedReferences prctl.set_name('networkd_main') prctl.set_proctitle('networkd') options.parse_command_line() dm = DeviceManager() application = Application([ (r"/devices", DeviceInfoHandler, dict(ethmanager=dm)), (r"/devices/([0-9]+)", DeviceInfoHandler, dict(ethmanager=dm)), (r"/devices/actions/(.+)", DevicesActionHandler, dict(ethmanager=dm)), (r"/devices/([0-9]+)/actions/(.+)", DeviceActionHandler, dict(ethmanager=dm)), ]) application.listen(8888) IOLoop.instance().start()
def give_name2thread(name, thread_obj): """Give name to current thread. if prctl installed, then also set name via this module. (Then you can see thread name in htop .) Do not delete calls of this function. Thread name useful for searching bugs. :param name: thread name :type name: str :param thread_obj: pass thread object to legacy python function :type thread_obj: Thread """ thread_obj.name = name if prctl: prctl.set_name(name)
def run(self): if prctlimported: prctl.set_name("Tribler"+currentThread().getName()) try: self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.connect(("127.0.0.1",self.port)) while True: data = self.sock.recv(10240) if len(data) == 0: break self.data_came_in(data) except: print_exc() self.close()
def run(self): if prctlimported: prctl.set_name("Tribler" + currentThread().getName()) if not self.initComplete: self.init() if PROFILE: fname = "profile-%s" % self.getName() import cProfile cProfile.runctx("self._run()", globals(), locals(), filename=fname) import pstats print >> sys.stderr, "profile: data for %s" % self.getName() pstats.Stats(fname, stream=sys.stderr).sort_stats("cumulative").print_stats(20) else: self._run()
def run(self): if prctlimported: prctl.set_name("Tribler"+threading.currentThread().getName()) while not self.stop_dht: try: data, addr = self.socket.recvfrom(1024) except (socket.timeout): #droid.log('DHT alive %d' % self.rand_num) pass except: #droid.log('EXCEPTION in recvfrom') pass else: self.stop_dht = self.handle(data, addr) or self.stop_dht self.pymdht.stop()
def loop(self): # set thread name (visible from ps and top) set_name(self._name[:16]) # from now on we will assume GET_IDENT() is the running thread self._thread_ident = get_ident() with self._lock: if self._state == "STATE_PLEASE_RUN": self._state = "STATE_RUNNING" logger.debug("STATE_RUNNING") # handle tasks as long as possible while self._one_task(): pass self._shutdown()
def run(self): if prctlimported: prctl.set_name("Tribler"+currentThread().getName()) announce = self._tracker + "?" + urlencode({"info_hash":self._swarm.get_info_hash(), "peer_id":self._swarm.get_peer_id(), "port":"12345", "compact":"1", "uploaded":"0", "downloaded":"0", "left":"-1", "event":"started"}) try: body = bdecode(urlopen(announce).read()) except: pass else: if "failure reason" in body: return if not "peers" in body: return # using low-bandwidth binary format peers = [] peer_data = body["peers"] if not isinstance(peer_data, str): return for x in range(0, len(peer_data), 6): # key = peer_data[x:x+6] ip = ".".join([str(ord(i)) for i in peer_data[x:x+4]]) port = (ord(peer_data[x+4]) << 8) | ord(peer_data[x+5]) peers.append((ip, port)) if DEBUG: print >> sys.stderr, "MiniTracker.run() received", len(peers), "peer addresses from tracker" self._swarm.add_potential_peers(peers)
def walk_log(self, network_name, log_path, log_file, log_meta): log_file_full = os.path.join(log_path, log_file) print u"Parsing {0}/{1}".format(network_name, log_file) prctl.set_name("regex worker") with codecs.open(log_file_full, 'rb', 'utf-8', 'replace') as log: # Somehow a \x1d got interpreted as a newline character. What? log_split = log.read().split("\n") for line_no, line in enumerate(log_split): # ??? if line == u'': continue line_data = self.line_matcher.match(line) if line_data is None: self.errors.append("Line {0}: ``{1!r}'' at {2} {3}".format(line_no, line, network_name, log_meta)) print self.errors[-1] continue time = datetime( int(log_meta['year']), int(log_meta['month']), int(log_meta['day']), int(line_data['hh']), int(line_data['mm']), int(line_data['ss']), ) self.index_pool.submit( self.attach_and_index, line_no=line_no + 1, network=network_name, channel=log_meta['target'], author=line_data.get('author', ''), time=time, content=line_data.get('content', '') )
def run(self): """ Until told to quit, retrieve the next task and execute it, calling the callback if any. """ if prctlimported: prctl.set_name("Tribler" + threading.currentThread().getName()) # Arno, 2010-04-07: Dying only used when shrinking pool now. while self.__isDying == False: # Arno, 2010-01-28: add try catch block. Sometimes tasks lists grow, # could be because all Threads are dying. try: cmd, args, callback = self.__pool.getNextTask() if cmd is None: break elif callback is None: cmd(*args) else: callback(cmd(args)) except: print_exc()
def setprocname(name): """Set the process name if possible. Requires setproctitle (python-setproctitle) from https://github.com/dvarrazzo/py-setproctitle (preferred) or python-prctl (debian package) from https://github.com/seveas/python-prctl . """ try: import setproctitle setproctitle.setproctitle(name) except ImportError: try: import prctl # for ps and top, up to 16 bytes long prctl.set_name(name) # for ps aux and top -c # will silently truncate to **argv (see docs) prctl.set_proctitle(name) except ImportError: return
def main(): for opt in sys.argv[1:]: if opt in ('-h', '--help'): usage() return elif opt in ('-d', '--dev'): config.ENABLE_INSPECTOR = True else: print "hotot: unrecognized option '%s'" % opt usage() sys.exit(1) try: import i18n except: from gettext import gettext as _ try: import prctl prctl.set_name('hotot') except: pass #g_thread_init has been deprecated since version 2.32 if GLib.check_version(2, 32, 0): GObject.threads_init() Gdk.threads_init() Gtk.init(None) config.init(); agent.app = Hotot() Gdk.threads_enter() Gtk.main() Gdk.threads_leave()
#!python_rand_hash import os, sys if "runserver" in sys.argv: # Always do this in debug mode. os.environ["DEBUG"] = "1" else: # Put the managment command name into the process name. try: import prctl prctl.set_name(sys.argv[1]) except: pass if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
def daemonize(self): """ do the UNIX double-fork magic, see Stevens' "Advanced Programming in the UNIX Environment" for details (ISBN 0201563177) http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 """ if self.interactive: return try: pid = os.fork() if pid > 0: # exit first parent sys.exit(0) except OSError as e: sys.stderr.write("fork #1 failed: {0:d} ({1})\n".format(e.errno, e.strerror)) raise DaemonError("Fork #1 failed") # decouple from parent environment os.chdir("/") os.setsid() os.umask(0) # do second fork try: pid = os.fork() if pid > 0: # exit from second parent sys.exit(0) except OSError as e: sys.stderr.write("fork #2 failed: {0:d} ({1})\n".format(e.errno, e.strerror)) raise DaemonError("Fork #2 failed") # redirect standard file descriptors sys.stdout.flush() sys.stderr.flush() self.stderr_mode = 'w+' si = open(self.stdin, 'r') so = open(self.stdout, self.stdout_mode) if 'b' in self.stderr_mode.lower(): se = open(self.stderr, self.stderr_mode, 0) else: se = open(self.stderr, self.stderr_mode) os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) # set name if self.name is not None: try: if HAS_PRCTL: prctl.set_name(self.name) prctl.set_proctitle(self.name) else: from ctypes import cdll libc = cdll.LoadLibrary("libc.so.6") libc.prctl(15, self.name, 0, 0, 0) except: pass # write lock atexit.register(self._release_lock) self.lock_file.acquire(blocking=self.lock_blocking, timeout=self.lock_timeout) self._write_lock_data()
def test_name(self): """Test setting the process name""" name = prctl.get_name().swapcase() * 16 prctl.set_name(name) self.assertEqual(prctl.get_name(), name[:15])
def run(self): """ Run by server thread """ if prctlimported: prctl.set_name("Tribler"+currentThread().getName()) while True: task = None timeout = None flag = False self.cond.acquire() while True: while len(self.queue) == 0 or flag: flag = False if timeout is None: # Wait until something is queued self.cond.wait() else: # Wait till first event is due self.cond.wait(timeout) # A new event was added or an event is due self.queue.sort() (when,count,task,id) = self.queue[0] if DEBUG: print >>sys.stderr,"ttqueue: EVENT IN QUEUE",when,task now = time() if now < when: # Event not due, wait some more if DEBUG: print >>sys.stderr,"ttqueue: EVENT NOT TILL",when-now timeout = when-now flag = True else: # Event due, execute if DEBUG: print >>sys.stderr,"ttqueue: EVENT DUE" self.queue.pop(0) if __debug__: assert count in self.callstack stack = self.callstack.pop(count) break self.cond.release() # Execute task outside lock try: # 'stop' and 'quit' are only used for unit test if task == 'stop': break elif task == 'quit': if len(self.queue) == 0: break else: (when,count,task,id) = self.queue[-1] t = when-time()+0.001 self.add_task('quit',t) else: if self.inDEBUG: t1 = time() task() if self.inDEBUG: took = time() - t1 if took > 0.2: debug_call_name = task.__name__ if hasattr(task, "__name__") else str(task) print >> sys.stderr,"ttqueue: EVENT TOOK", took, debug_call_name except: print_exc() if __debug__: print >> sys.stderr, "<<<<<<<<<<<<<<<<" print >> sys.stderr, "TASK QUEUED FROM" print >> sys.stderr, "".join(stack) print >> sys.stderr, ">>>>>>>>>>>>>>>>"
#!.env/bin/python -R import os, sys try: import prctl prctl.set_name(os.environ["NAME"] + "-django") except: pass if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
import os, prctl os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") prctl.set_name("govtrack-" + os.environ["NAME"]) from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
def _thread_entry(): if prctl: prctl.set_name('mirte manager') m.run() l.info('manager.run() returned')
def _create_and_seed_metadata(self, videofile, torrent): from Tribler.Main.vwxGUI.GuiUtility import GUIUtility if prctlimported: prctl.set_name("Tribler"+currentThread().getName()) self.guiutility = GUIUtility.getInstance() self.session = self.guiutility.utility.session videoanalyser = self.session.get_video_analyser_path() torcoldir = self.session.get_torrent_collecting_dir() rel_thumbdir = 'thumbs-'+binascii.hexlify(torrent.infohash) abs_thumbdir = os.path.join(torcoldir, rel_thumbdir) videoname = os.path.basename(videofile) if os.path.exists(abs_thumbdir): if DEBUG: print >> sys.stderr, 'create_and_seed_metadata: already downloaded thumbnails for torrent', torrent.name return if DEBUG: print >> sys.stderr, 'create_and_seed_metadata: going to seed metadata for torrent', torrent.name duration, bitrate, resolution = get_videoinfo(videofile, videoanalyser) video_info = {'duration': duration, \ 'bitrate': bitrate, \ 'resolution': resolution} if DEBUG: print >> sys.stderr, 'create_and_seed_metadata: FFMPEG - duration = %d, bitrate = %d, resolution = %s' % (duration, bitrate, resolution) if not os.path.exists(abs_thumbdir): os.makedirs(abs_thumbdir) thumb_filenames = [os.path.join(abs_thumbdir, videoname + postfix) for postfix in ["-thumb%d.jpg" % i for i in range(1,5)]] thumb_resolutions = [(1280, 720), (320, 240), (320, 240), (320, 240)] thumb_timecodes = preferred_timecodes(videofile, duration, limit_resolution(resolution, (100, 100)), videoanalyser, k = 4) for filename, max_res, timecode in zip(thumb_filenames, thumb_resolutions, thumb_timecodes): thumb_res = limit_resolution(resolution, max_res) get_thumbnail(videofile, filename, thumb_res, videoanalyser, timecode) if DEBUG: path_exists = os.path.exists(filename) print >> sys.stderr, 'create_and_seed_metadata: FFMPEG - thumbnail created = %s, timecode = %d' % (path_exists, timecode) sdef = SwiftDef() sdef.set_tracker("127.0.0.1:9999") for thumbfile in thumb_filenames: if os.path.exists(thumbfile): xi = os.path.relpath(thumbfile, torcoldir) if sys.platform == "win32": xi = xi.replace("\\","/") si = xi.encode("UTF-8") sdef.add_content(thumbfile, si) specpn = sdef.finalize(self.session.get_swift_path(), destdir = torcoldir) hex_roothash = sdef.get_roothash_as_hex() try: swift_filename = os.path.join(torcoldir, hex_roothash) shutil.move(specpn, swift_filename) shutil.move(specpn+'.mhash', swift_filename+'.mhash') shutil.move(specpn+'.mbinmap', swift_filename+'.mbinmap') except: if DEBUG: print_exc() modifications = {'swift-thumbnails': json.dumps((thumb_timecodes, sdef.get_roothash_as_hex())), \ 'video-info': json.dumps(video_info)} if DEBUG: print >> sys.stderr, 'create_and_seed_metadata: modifications =', modifications self.channelsearch_manager.modifyTorrent(torrent.channel.id, torrent.channeltorrent_id, modifications)
import os, prctl os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") try: prctl.set_name("django-" + os.environ["NAME"]) except: pass from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
def run(self): """ Gets one torrent from good or unknown list and checks it """ if prctlimported: prctl.set_name("Tribler" + currentThread().getName()) # request new infohash from queue, start database while not self.shouldquit: start = time() self.sleepEvent.clear() try: torrent = None try: self.queueLock.acquire() while True: torrent = self.queue.popleft() self.queueset.discard(torrent['infohash']) if DEBUG: print >> sys.stderr, "TorrentChecking: get value from QUEUE:", torrent break except: pass finally: self.queueLock.release() if torrent: self.dbDoCheck(torrent) else: self.dbSelectTorrentToCheck(self.dbDoCheck) torrent = self.announceQueue.get() if torrent: if DEBUG: print >> sys.stderr, "TorrentChecking: tracker checking", torrent["info"].get("announce", ""), torrent["info"].get("announce-list", "") trackerStart = time() multi_announce_dict = multiTrackerChecking(torrent, self.getInfoHashesForTracker) if DEBUG: print >> sys.stderr, "TorrentChecking: tracker checking took ", time() - trackerStart, torrent["info"].get("announce", ""), torrent["info"].get("announce-list", "") if not self.shouldquit: # Modify last_check time such that the torrents in queue will be skipped if present in this multi-announce with self.queueLock: for tor in self.queue: if tor['infohash'] in multi_announce_dict: tor['last_check'] = time() # Update torrent with new status self.dbUpdateTorrents(torrent, multi_announce_dict) self.announceQueue.task_done() except: # make sure we do not crash while True loop print_exc() # schedule sleep time, only if we do not have any infohashes scheduled if len(self.queue) == 0 and not self.shouldquit: diff = time() - start remaining = int(self.interval - diff) if remaining > 0: if DEBUG: print >> sys.stderr, "TorrentChecking: going to sleep for", remaining self.sleepEvent.wait(remaining)