def add_jobs_segment(powerline): num_jobs = 0 if platform.system().startswith('CYGWIN'): # cygwin ps is a special snowflake... output_proc = subprocess.Popen(['ps', '-af'], stdout=subprocess.PIPE) output = map(lambda l: int(l.split()[2].strip()), output_proc.communicate()[0].decode("utf-8").splitlines()[1:]) num_jobs = output.count(os.getppid()) - 1 else: pppid_proc = subprocess.Popen(['ps', '-p', str(os.getppid()), '-oppid='], stdout=subprocess.PIPE) pppid = pppid_proc.communicate()[0].decode("utf-8").strip() output_proc = subprocess.Popen(['ps', '-a', '-o', 'ppid'], stdout=subprocess.PIPE) output = output_proc.communicate()[0].decode("utf-8") num_jobs = len(re.findall(str(pppid), output)) - 1 if num_jobs > 0: powerline.append(' %d ' % num_jobs, Color.JOBS_FG, Color.JOBS_BG)
def add_jobs_segment(): if "Linux" in platform.system(): pppid = subprocess.Popen( [ 'ps', '-p', str(os.getppid()), '-oppid=' ], stdout=subprocess.PIPE).communicate()[0].strip() output = str(subprocess.Popen( [ 'ps', '-a', '-o', 'ppid' ], stdout=subprocess.PIPE).communicate()[0]) num_jobs = len(re.findall(str(pppid), output)) - 1 elif "CYGWIN" in platform.system(): # PS under Cygwin doesn't support the "-o" option. Have to parse it ourselves. ps_out = subprocess.Popen(['ps', '-p', str(os.getppid())], stdout=subprocess.PIPE).communicate()[0] pppid = re.search(r'(\d+)', ps_out.split('\n')[1]).group(0) output = subprocess.Popen(['ps', '-a'], stdout=subprocess.PIPE).communicate()[0] jobs = [line.split() for line in output.split("\n")][1:] pids = [job[1] if job else 0 for job in jobs] num_jobs = sum(pid == pppid for pid in pids) - 1 # -1 for the instance of PS being run else: # This _should_ never happen. What are you running this under? return if num_jobs > 0: powerline.append(' %d ' % num_jobs, Color.JOBS_FG, Color.JOBS_BG)
def main(): """etcd driver main entry point. Implementation note: this is implemented as a function to allow it to be imported and executed from the pyinstaller launcher. Without the extra indirection, pyilauncher would deadlock when it tried to import this module. """ last_ppid = os.getppid() common.default_logging(gevent_in_use=False, syslog_executable_name="calico-felix-etcd") felix_sck = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: felix_sck.connect(sys.argv[1]) except: _log.exception("Failed to connect to Felix") raise etcd_driver = driver.EtcdDriver(felix_sck) etcd_driver.start() while not etcd_driver.join(timeout=1): parent_pid = os.getppid() # Defensive, just in case we don't get a socket error, check if the # parent PID has changed, indicating that Felix has died. if parent_pid == 1 or parent_pid != last_ppid: _log.critical("Process adopted, assuming felix has died") etcd_driver.stop() break _log.critical("Driver shutting down.")
def run(self): self.logs.debug('线程.开始执行 name %s pid %d ' % (self.t_name,os.getppid())) if self.__zmq_Initialization(): return try: while True: self.logs.info('开始等待接收信息 - 第%s次' %(self.Counter)) msg = self.__recv() #if self.__mode_Verification(msg): # break ass,apps = self.__Overload() if ass: if hasattr(apps, '%s'%msg['mode']): data = getattr(apps, '%s'%msg['mode'])(msg) else: data={'mode':False,'data':{'message':'没有这个参数'}} else: data={'mode':False,'data':{'message':'系统发生异常错误.请提交管理员后进行确认'}} self.s.send_json(data) self.Counter += 1 time.sleep(1) self.logs.debug('线程.结束执行 name %s pid %d ' % (self.t_name,os.getppid())) except Exception as e : print e self.logs.warning('多线程错误信息 %s'%e) pass
def run( polling=None ,dir_src=None ,dir_dst=None ,dir_tmp=None ,exit_error=False ,delete_source=False ,**_): dir_src=handle_path(dir_src) dir_dst=handle_path(dir_dst) dir_tmp=handle_path(dir_tmp) ppid=os.getppid() logging.info("Process pid: %s" % os.getpid()) logging.info("Parent pid: %s" % ppid) logging.info("Starting loop...") while True: if os.getppid()!=ppid: logging.warning("Parent terminated... exiting") break do_processing(delete_source, exit_error, dir_src, dir_dst, dir_tmp) logging.debug("...sleeping for %s seconds" % polling) sleep(polling)
def tsGetProcessParameters(self, theInfo): ''' ''' pid = os.getpid() theInfo += ['\n'] theInfo += [' Process Parameters'] theInfo += [' ------------------'] theInfo += [' pid = <%d> / <0x%X>' % (pid, pid)] ## if self.theSystem != 'Windows': try: # The following code supports Cygwin, Linux, Mac OS X and Unix. # # The platform.windows function only works if Mark Hammond's # win32all package is installed and (obviously) only runs # on Win32 compatible platforms. theInfo += [' getppid = <%d> / <0x%X>' % (os.getppid(), os.getppid())] theInfo += [' getegid = <%s>' % os.getegid()] theInfo += [' geteuid = <%s>' % os.geteuid()] theInfo += [' getgid = <%s>' % os.getgid()] theInfo += [' getgroups = <%s>' % os.getgroups()] theInfo += [' getpgid = <%s>' % os.getpgid(pid)] theInfo += [' getuid = <%s>' % os.getuid()] try: theInfo += [' getlogin = <%s>' % os.getlogin()] except Exception as errorCode: self.logger.warning( 'tsPlatformRunTimeEnvironment.' + \ 'tsGetProcessParameters: errorCode=%s' % str(errorCode)) theInfo += [' getlogin = <%s>' % ( pwd.getpwuid(os.getuid())[0])] theInfo += [' ctermid = <%s>' % os.ctermid()] except Exception as errorCode: # Neither print function nor "from tsLogger import TsLogger" # are available in Python 2.3.4. pass theInfo += [' cwd = <%s>' % os.getcwd()] environ = os.environ theInfo += ['\n'] theInfo += [' Environment Variables'] theInfo += [' ---------------------'] keys = list(environ.keys()) keyList = sorted(keys) keyWidth = 0 for key in keyList: if len(key) > keyWidth: keyWidth = len(key) if len(keyList) == 0: theInfo += [' environment = <>'] else: for key in sorted(keys): theInfo += [' %s = <%s>' % \ (key.rjust(keyWidth), environ[key])]
def start(): handleUserDefinedSignals(childWaitSigHandler) print "forking ..." pid=os.fork() print "IBSng started with pid=%d"%pid if pid == 0: try: try: mainThreadInitialize() os.kill(os.getppid(),signal.SIGUSR1) except: print "Shutting down on error" os.kill(os.getppid(),signal.SIGUSR2) raise except: err_text=ibs_exceptions.getExceptionText() print err_text logToSysLog(err_text) core.main.mainThreadShutdown() print "Successfully initialized, entering event loop ..." event.startLoop() else: signal.pause()
def upload(configobj, smugmug, lock): """ This method will upload all the missing files 5 at a time. """ myLogger.info("upload() parent process:'{0}' process id:'{1}".format(os.getppid(),os.getpid())) start = time.time() myLogger.debug('upload - parent process: %s process id: %s', os.getppid(), os.getpid()) conn = db.getConn(configobj) sync = datetime.now() myLogger.debug("getting images to upload") uploadImages = db.imagesToUpload(conn) myLogger.debug("Have list of images that need to be uploaded.") #Loop through the images and download them queue = Queue.Queue() #spawn a pool of threads, and pass them queue instance threads = [] for i in range(3): t = UploadThread(queue, conn, configobj, smugmug, lock) t.start() threads.append(t) #populate queue with data size = 0 for uploadImage in uploadImages: queue.put(uploadImage) size = size + 1 print size #wait on the queue until everything has been processed queue.join() end = time.time() myLogger.debug("queue emptied in %d", (end - start)) #stop the threads myLogger.debug("finished uploading images. it took %d", (end - start))
def worker(event): print '..........(worker start)', '[worker](pid:%s ppid:%s)' % (os.getpid(), os.getppid()) time.sleep(random.random()) event.wait() print '==========(internal flag is true)', '[worker](pid:%s ppid:%s)' % (os.getpid(), os.getppid()) time.sleep(random.random()) print '..........(worker stop)', '[worker](pid:%s ppid:%s)' % (os.getpid(), os.getppid())
def run( module_name=None ,function_name=None ,fargs=None ,polling_interval=None ,**_kw ): logging.debug("Preparing callable...") try: _mod, fn=prepare_callable(module_name, function_name) except: raise Exception("Can't prepare callable: %s.%s" % (module_name, function_name)) ppid=os.getppid() logging.info("Process pid: %s" % os.getpid()) logging.info("Parent pid : %s" % ppid) logging.info("Starting loop...") while True: if os.getppid()!=ppid: logging.warning("Parent terminated... exiting") break try: result=fn(*fargs) except Exception, e: logging.warning("Problem with callable: %s" % str(e)) if result is not None: stdout(result) stdout_flush() logging.debug("...sleeping for %s seconds" % polling_interval) time.sleep(polling_interval)
def run(self): proc_name = self.name parentPID = os.getppid() while True: if os.getppid() != parentPID: break # parent died this_job = self.task_queue.get() if this_job is None: self.task_queue.task_done() break # kill signal this_input = this_job['input'] this_task = self.task_class this_data = this_task(*this_input) this_job['result'] = this_data self.result_queue.put(this_job) self.task_queue.task_done() #: while alive return
def restart(opts = (), flush = True): from dropbox.client.high_trace import force_flush try: TRACE('Restarting with args %r', opts) except Exception: pass try: if flush: force_flush() except Exception: unhandled_exc_handler(False) argv = executable(opts) try: childpid = os.fork() except Exception: unhandled_exc_handler() _fallback_restart(argv) else: if childpid: time.sleep(5) os._exit(0) else: if os.getppid() != 1: try: os.kill(os.getppid(), signal.SIGKILL) except Exception: pass _set_all_fds_cloexec() try: os.execv(argv[0], argv) except Exception: _fallback_restart(argv)
def run(self): if not self.runable: print 'Process not runable, returning' return False # Drop privileges os.setgroups([]) os.setgid(self.gid) os.setuid(self.uid) ppid = os.getppid() while True: try: line = self.work_queue.get(timeout=0.5) if not line: 'Parent process is asking us to exit' return True line = line.decode('utf-8').encode('ASCII', 'ignore') except KeyboardInterrupt: return False except UnicodeDecodeError: print 'Unicode Error, skipping entry' continue except QueueEmpty: if os.getppid() != ppid: return False continue try: entry = SyslogEntry.from_line(line) except pyparsing.exceptions.Exception: continue self.process_entry(entry)
def profile_result(func): """ decorator for start,end event function profiling with netlogger. return value is logged as result. """ if os.getenv('NETLOGGER_ON', False) in ( 'off','0','no','false','',False): return func if type(func) is not types.FunctionType: return func if func.__module__ == '__main__': f = func.func_globals['__file__'] or 'unknown' event = '%s' %os.path.splitext(os.path.basename(f))[0] log = _logger('script') log.set_meta(file=f, pid=os.getpid(), ppid=os.getppid(), gpid=os.getgid()) else: event = '%s' %func.func_name log = _logger('%s' %func.__module__) log.set_meta(pid=os.getpid(), ppid=os.getppid(), gpid=os.getgid()) def nl_profile_func(*args, **kw): log.debug("%s.start" %event) try: v = func(*args, **kw) except: log.error("%s.end" %event) raise log.debug("%s.end" %event, result=v) return v return nl_profile_func
def run(self, token, task): ppid = os.getppid() response = {} while True: time.sleep(self._interval) if os.getppid() != ppid: os._exit(1) try: logger.info('heartbeat {} for task {}'.format( time.time(), task.activity_type.name)) except: # Do not crash for debug pass try: response = self.send_heartbeat(token) except swf.exceptions.DoesNotExistError: # Either the task or the workflow execution no longer exists. logger.warning( 'task {} no longer exists. Stopping heartbeat'.format( task.activity_type.name)) return except Exception as error: # Let's crash if it cannot notify the heartbeat failed. logger.error('cannot send heartbeat for task {}: {}'.format( task.activity_type.name, error)) raise if response and response.get('cancelRequested'): return
def tsGetProcessParameters(self, theInfo): ''' ''' pid = os.getpid() theInfo += ['\n'] theInfo += [' Process Parameters'] theInfo += [' ------------------'] theInfo += [' pid = <%d> / <0x%X>' % (pid, pid)] ## if self.theSystem != 'Windows': try: # The following code supports Cygwin, Linux, Mac OS X and Unix. # # The platform.windows function only works if Mark Hammond's # win32all package is installed and (obviously) only runs # on Win32 compatible platforms. theInfo += [' getppid = <%d> / <0x%X>' % (os.getppid(), os.getppid())] theInfo += [' getegid = <%s>' % os.getegid()] theInfo += [' geteuid = <%s>' % os.geteuid()] theInfo += [' getgid = <%s>' % os.getgid()] theInfo += [' getgroups = <%s>' % os.getgroups()] theInfo += [' getpgid = <%s>' % os.getpgid(pid)] theInfo += [' getuid = <%s>' % os.getuid()] try: theInfo += [' getlogin = <%s>' % os.getlogin()] except Exception, errorCode: self.logger.warning( 'tsPlatformRunTimeEnvironment.' + \ 'tsGetProcessParameters: errorCode=%s' % str(errorCode)) theInfo += [' getlogin = <%s>' % ( pwd.getpwuid(os.getuid())[0])] theInfo += [' ctermid = <%s>' % os.ctermid()]
def daemonize(): """ daemonize: Purpose: Detach from stdin/stdout/stderr, return control of the term to the user. Returns: Nothing. """ if os.name == "nt": # No way to fork or daemonize on windows. Just do nothing for now? return if not os.fork(): # get our own session and fixup std[in,out,err] os.setsid() sys.stdin.close() sys.stdout = NullDevice() sys.stderr = NullDevice() if not os.fork(): # hang around till adopted by init ppid = os.getppid() while ppid != 1: time.sleep(0.5) ppid = os.getppid() else: # time for child to die os._exit(0) else: # wait for child to die and then bail os.wait() sys.exit()
def watch_parent(): """ Thread to watch for the parent pid. If this process has been orphaned it means middlewared process has crashed and there is nothing left to do here other than commit suicide! """ kqueue = select.kqueue() try: kqueue.control([ select.kevent( os.getppid(), filter=select.KQ_FILTER_PROC, flags=select.KQ_EV_ADD, fflags=select.KQ_NOTE_EXIT, ) ], 0, 0) except ProcessLookupError: os._exit(1) while True: ppid = os.getppid() if ppid == 1: break kqueue.control(None, 1) os._exit(1)
def run(self): context = zmq.Context() sink_socket = context.socket(zmq.PUSH) sink_socket.connect('tcp://%s:%d' % settings.SPROBE_SINK) total_sleep = 0 while True: if (os.getppid() != self.ppid): logger.info('ppids: %d, %d' % (os.getppid(), self.ppid)) for w in self.workers.keys(): worker = self.workers[w] if (worker.is_alive()): #@todo: signal worker to cleanup and exit. worker.task['queue'].put('stop') break for w in self.workers.keys(): if (not self.workers[w].is_alive()): to = Task.objects.get(id=w) if (self.workers[w].exitcode == 0): to.state = 'finished' else: to.state = 'error' to.end = datetime.utcnow().replace(tzinfo=utc) data = serialize("json", (to,)) sink_socket.send_json(data) del(self.workers[w]) if (total_sleep == 60): for td in TaskDefinition.objects.all(): now = datetime.utcnow().replace(second=0, microsecond=0, tzinfo=utc) if (self._schedulable(td, now)): t = Task(name=td.name, json_meta=td.json_meta, state='scheduled', start=now) data = serialize("json", (t,)) sink_socket.send_json(data) total_sleep = 0 for t in Task.objects.filter(state='scheduled'): worker = TaskWorker(t) self.workers[t.id] = worker worker.daemon = True worker.start() if (worker.is_alive()): t.state = 'running' data = serialize("json", (t,)) sink_socket.send_json(data) else: t.state = 'error' t.end = datetime.utcnow().replace(tzinfo=utc) data = serialize("json", (t,)) sink_socket.send_json(data) time.sleep(1) total_sleep = total_sleep + 1 sink_socket.close() context.term() logger.info('terminated context. exiting')
def stop(self): """Shut down all hitchserve services and processes started from driver process, kill if necessary.""" if not self._shutdown_triggered: self._shutdown_triggered = True self.ipython_on = False start_shutdown_time = time.time() for child in psutil.Process(os.getppid()).children(): if child is not None and child.is_running() and child.pid != os.getpid(): for grandchild in psutil.Process(child.pid).children(recursive=True): try: grandchild.send_signal(signal.SIGINT) self.logline("Stopping PID {} : {}".format(grandchild.pid, " ".join(grandchild.cmdline()))) except psutil.NoSuchProcess: pass try: child.send_signal(signal.SIGINT) self.logline("Stopping PID {} : {}".format(child.pid, " ".join(child.cmdline()))) except psutil.NoSuchProcess: pass self.service_engine.stop() still_alive = False if time.time() < start_shutdown_time + self.service_bundle.shutdown_timeout: sleep_time = self.service_bundle.shutdown_timeout - (time.time() - start_shutdown_time) for i in range(0, int(sleep_time * 100)): still_alive = False for child in psutil.Process(os.getppid()).children(): if child is not None and child.is_running() and child.pid != os.getpid(): for grandchild in psutil.Process(child.pid).children(recursive=True): try: if grandchild.status != "zombie": still_alive = True except psutil.NoSuchProcess: pass if not still_alive: break time.sleep(0.01) if still_alive: for child in psutil.Process(os.getppid()).children(): if child is not None and child.is_running() and child.pid != os.getpid(): for grandchild in psutil.Process(child.pid).children(recursive=True): try: fullname = " ".join(grandchild.cmdline()) grandchild.send_signal(signal.SIGKILL) self.logline("Killing PID {} : {}".format(grandchild.pid, grandchild.name)) except psutil.NoSuchProcess: pass try: child.send_signal(signal.SIGKILL) self.logline("Killing PID {} : {}".format(child.pid, child.name())) except psutil.NoSuchProcess: pass self.logline("Shutdown successful in {0:.1f} seconds".format(time.time() - start_shutdown_time)) self._close_pipes()
def main(): f = open("/tmp/daemon-log", "w") print os.getppid(),"ppid\n" print os.getpid(), "pid\n" while True: f.write("%s/n"%time.ctime(time.time())) f.flush() time.sleep(10)
def parallel(d, name): time.sleep(1) lock.acquire() print 'hello--'+str(name) print os.getppid(), '-----------', os.getpid() d[name] = name * name print d lock.release()
def watch_parent(): parent = os.getppid() # If the parent process gets kills, this process will be attached to init. # In that case the plugin should stop running. while True: if os.getppid() != parent: os._exit(1) time.sleep(1)
def __init__(self): import os, database, time self.regex = "^uptime$" self.ppid = os.getppid() database.doSQL("delete from data where type = 'uptime' and created_by != '" + str(os.getppid()) + "'"); if len(database.doSQL("select * from data where type = 'uptime' and created_by ='" + str(os.getppid()) + "'")) == 0: database.doSQL("insert into data values('" + str(int(time.time())) + "', 'uptime', '" + str(os.getppid()) + "')")
def get_walls(token, item_ids): print "Start " + str(os.getppid()) + " " + str(os.getpid()) # create instance of vkrequest vk_requests = VkRequests(token, API_VERSION, RANDOM_METHOD_NAMES) print "Total walls to get: {amount}".format(amount=len(item_ids)) i = 0 # create instance to write to DB datawriter = DataWriter(DB_NAME, True) # constants BULK_SIZE = 25 # amount of requests in vksctipt CODE_METHOD_NAME = "wall.get" # request name CHANGE_VAR = "owner_id" METHOD_NAME = "execute" for i in range(0, len(item_ids)/BULK_SIZE+1): # add time lag to avoid bans time.sleep(0.33) walls_info = [] # get ids portion ids_bulk = item_ids[i*BULK_SIZE:(i+1)*BULK_SIZE] # create vkscript request vk_code = vk_requests.script_request(CODE_METHOD_NAME, CHANGE_VAR, ids_bulk) # add params to request fix_params = "code={code}&count=100".format(code=vk_code) # send request wtih vkscript to vk request = vk_requests.single_request(method_name=METHOD_NAME, fix_params=fix_params, token_flag=True) response = vk_requests.make_single_request(request) print "Process: {proc} Getting publics walls: ({size}/{total})".format( proc=str(os.getpid()), size=i*BULK_SIZE, total=len(item_ids)) # add public_id field to response and count failed items j = 0 failed = 0 for item in response: if not isinstance(item, bool): item["public_id"] = ids_bulk[j] walls_info.append(item) else: failed += 1 j += 1 # save to db datawriter.write_items_db("publics_walls", walls_info, create_index=True, index_fields=["public_id"]) # send random request every 3rd request to avoid bans if not i % 3: vk_requests.make_single_request(fake=True) print "End " + " " + str(os.getppid()) + " " + str(os.getpid())
def run(prefix=None, polling_interval=None ,force_kill=None ,username=None ,ppid=None ,**_ ): def prefix_match(entry): if prefix is None: return True bns=map(os.path.basename, entry.cmdline) flist=filter(lambda x:x.startswith(prefix), bns) return len(flist)>0 def filtre(pentry): user_match=(username is None) or (username==pentry.username) name_match=(prefix is None) or (pentry.name.startswith(prefix)) ppid_match=(ppid is None) or (pentry.ppid==ppid) pmatch=prefix_match(pentry) return ppid_match and (name_match or pmatch) and user_match liste=[] this_pid=os.getpid() this_ppid=os.getppid() logging.info("Process pid: %s" % this_pid) logging.info("Parent pid: %s" % this_ppid) logging.info("Starting loop...") while True: if os.getppid()!=this_ppid: logging.warning("Parent terminated... exiting") break #deprecated in psutil #plist=psutil.get_process_list() #flist=filter(filtre, plist) for proc in psutil.process_iter(): ## make sure we get a fresh copy p=psutil.Process(proc.pid) if filtre(p): cmdline=p.cmdline ## skip ourself! if p.pid != this_pid: user=p.username details="pid '%s' '%s' : %s" % (p.pid, user, cmdline) do_kill(p.pid, details, force_kill) logging.debug("Sleeping...") sleep(polling_interval)
def main(): """ main entry """ sig_ids = [2, 4, 6, 8, 11, 15] mainProcID = os.getppid() print 'main procID:', mainProcID usage = "usage not ready yet \n" parser = OptionParser(usage=usage) parser.add_option("-d", "--dbus", dest="dbus", action='store_true', help="example") (options, args) = parser.parse_args() if not len(args) == 0: print args if options.dbus: is_dbus = True print is_dbus p_list = ['dbus_server', 'dbus_recv', 'dbus_emit'] AA = ATE_assist() AA.mainProcID = mainProcID for sig in sig_ids: signal.signal(sig, AA.sighandle) AA.myFork(p_list) try: currProcID = os.getppid() if currProcID == mainProcID: procID, procStaus = os.waitpid(AA.p_arr['dbus_emit'], 0) if procStaus == 0: print 'process %s quit normally : %s' % (procID, procStaus) for p in p_list: if not p == 'dbus_emit': print 'killing %s of pid %s' % (p, AA.p_arr[p]) os.kill(AA.p_arr[p], signal.SIGTERM) else: print 'process %s quit abnormally : %s' % (procID, procStaus) for p in p_list: if not p == 'dbus_emit': print 'killing %s of pid %s' % (p, AA.p_arr[p]) os.kill(AA.p_arr[p], signal.SIGTERM) except Exception, e: print 'Exception :', str(e) + str(os.getppid()) for p in p_list: print 'killing %s of pid %s' % (p, AA.p_arr[p]) os.kill(AA.p_arr[p], signal.SIGTERM)
def run(domain_name=None ,src_path=None ,polling=None ,batch_size=None ,category_name=None ,default_value=None ,src_ext=None ,reset_count=None ,**_ ): code, src_path=resolve_path(src_path) if not code.startswith("ok"): raise Exception("Can't resolve source path...") logging.info("Creating source path if required...") mkdir_p(src_path) ###################### db=SimpleKV() def connect(): db.connect() logging.info("Connecting to SDB... CTRL-C to abort") retry(connect) def create(): db.set_domain(domain_name) logging.info("Creating domain: %s... CTRL-C to abort" % domain_name) retry(create) ############# stage1=process1(db, batch_size, category_name, default_value) pollcount=0 # MAIN LOOP ########### ppid=os.getppid() logging.info("Process pid: %s" % os.getpid()) logging.info("Parent pid: %s" % ppid) logging.info("Starting loop...") while True: if os.getppid()!=ppid: logging.warning("Parent terminated... exiting") break reset=(pollcount==reset_count) code, files=get_root_files(src_path, only_ext=src_ext) if code.startswith("ok"): logging.debug("Got files: %s" % files) stage1.send((reset, files)) logging.debug("Sleeping...") sleep(polling) pollcount=pollcount+1
def process_square(num): pid = os.getpid() print('PPID:%s - PID:%s - NUM:%s - begin'%(os.getppid(), pid, str(num))) square = num * num sleep(2) print('PPID:%s - PID:%s - NUM:%s - SQUARE = %s'%(os.getppid(), pid, str(num), str(square) ) ) result_list.append( {"PID %s"%pid: square} )
def reIPLtrigger(anaconda): if not isS390(): return if anaconda.canReIPL: log.info("reIPL configuration successful => reboot") os.kill(os.getppid(), signal.SIGUSR2) else: log.info("reIPL configuration failed => halt") os.kill(os.getppid(), signal.SIGUSR1)
return pid if __name__ == "__main__": retCode = createDaemon() # The code, as is, will create a new file in the root directory, when # executed with superuser privileges. The file will contain the following # daemon related process parameters: return code, process ID, parent # process group ID, session ID, user ID, effective user ID, real group ID, # and the effective group ID. Notice the relationship between the daemon's # process ID, process group ID, and its parent's process ID. procParams = """ return code = %s process ID = %s parent process ID = %s process group ID = %s session ID = %s user ID = %s effective user ID = %s real group ID = %s effective group ID = %s """ % (retCode, os.getpid(), os.getppid(), os.getpgrp(), os.getsid(0), os.getuid(), os.geteuid(), os.getgid(), os.getegid()) open("/tmp/createDaemon.log", "w").write(procParams + "\n") sys.exit(retCode)
def sess_id(): """Return current-process id and father-process id""" return os.getpid(), os.getppid()
def info(title): print(title) print('module name : ', __name__) print("parnet process : ", os.getppid()) print("process id : ", os.getpid())
signal.signal(signal.SIGHUP, exit_handler) signal.signal(signal.SIGPIPE, exit_handler) signal.signal(signal.SIGQUIT, exit_handler) if len(sys.argv) < 3: inpipe = sys.stdin outpipe = sys.stdout else: inpipe = os.fdopen(int(sys.argv[1]), "r") outpipe = os.fdopen(int(sys.argv[2]), "w") try: while 1: # kill self if we get orphaned (tragic) ppid = os.getppid() if ppid == 1: raise RuntimeError("orphaned") setup_exit_handler() line = inpipe.readline() try: command = json.loads(line) except ValueError: raise RuntimeError("bad json parse") if command['action'] == "whatinstalled": query(command) elif command['action'] == "whatavailable": query(command)
def shell(self): """Get the current shell. @returns The current shell this process is running in. Examples: bash tcsh """ from rez.shells import get_shell_types shells = set(get_shell_types()) if not shells: raise RezSystemError("no shells available") if self.platform == "windows": return "cmd" else: import subprocess as sp shell = None # check parent process via ps try: args = ['ps', '-o', 'args=', '-p', str(os.getppid())] proc = sp.Popen(args, stdout=sp.PIPE) output = proc.communicate()[0] shell = os.path.basename(output.strip().split()[0]).replace( '-', '') except Exception: pass # check $SHELL if shell not in shells: shell = os.path.basename(os.getenv("SHELL", '')) # traverse parent procs via /proc/(pid)/status if shell not in shells: pid = str(os.getppid()) found = False while not found: try: file = os.path.join(os.sep, "proc", pid, "status") with open(file) as f: loc = f.read().split('\n') for line in loc: line = line.strip() toks = line.split() if len(toks) == 2: if toks[0] == "Name:": name = toks[1] if name in shells: shell = name found = True break elif toks[0] == "PPid:": pid = toks[1] except Exception: break if (shell not in shells) and ("sh" in shells): shell = "sh" # failed detection, fall back on 'sh' elif (shell not in shells) and ("bash" in shells): shell = "bash" # failed detection, fall back on 'bash' elif shell not in shells: shell = next(iter(shells)) # give up - just choose a shell # sh has to be handled as a special case if shell == "sh": if os.path.islink("/bin/sh"): path = os.readlink("/bin/sh") shell2 = os.path.split(path)[-1] if shell2 == "bash": # bash switches to sh-like shell when invoked as sh, # so we want to use the sh shell plugin pass elif shell2 == "dash": # dash doesn't have an sh emulation mode, so we have # to use the dash shell plugin if "dash" in shells: shell = "dash" else: # this isn't good! if "bash" in shells: shell = "bash" # fall back on bash else: shell = next(iter( shells)) # give up - just choose a shell # TODO: remove this when/if dash support added if shell == "dash": shell = "bash" return shell
---------------------------- * 了解操作系统的相关知识。 * Unix/Linux操作系统提供了一个fork()系统调用,它非常特殊, * 普通的函数调用,调用一次,返回一次,但是fork()调用一次,返回两次 * 因为操作系统自动把当前进程(称为父进程)复制了一份(称为子进程),然后,分别在父进程和子进程内返回 * 子进程永远返回0,而父进程返回子进程的ID。 * 这样做的理由是,一个父进程可以fork出很多子进程,所以,父进程要记下每个子进程的ID,而子进程只需要调用getppid()就可以拿到父进程的ID * os模块封装了常见的系统调用,其中就包括fork,可以在Python程序中轻松创建子进程 * fork(),仅仅在 Unix/Linux 上有效,Windows没用 import os print('Process (%s) start...' % os.getpid()) pid = os.fork() if pid == 0: print('我是子进程(%s)我的父进程是(%s).' %(os.getpid(), os.getppid())) else: print('我是父进(%s)创建了一个子进程(%s).' %(os.getpid(), pid)) -------------------------------------- 跨平台的多进程-multiprocessing.Process| -------------------------------------- * 模块:multiprocessing * multiprocessing 模块提供了一个 Process 类来代表一个进程对象 * 创建步骤 1,创建 Process 实例对象 Process() * 关键字参数 target # 目标函数,也就是多另起线程去执行的函数 args # 执行目标函数时,传递的参数,该值是一个 tuple
import os import time res = os.fork() if res == 0: n = 10 print("i am luke and i must kill darth vader in " + str(n) + "s") time.sleep(n) print("lightsaber attack!") vader = os.getppid() os.kill(vader, 9) else: while (True): time.sleep(1) print("i am darth vader")
def shutdown(self): return 'shutdown %d %d\n' % (os.getpid(), os.getppid())
def test2(interval): print('test2子进程运行中,pid=%d, 父进程pid=%d' % (os.getpid(), os.getppid())) t_start = time.time() time.sleep(interval) print('test2执行时间:%.2f秒' % (time.time() - t_start))
#子进程先退出, 父进程没有处理子进程的退出状态,此时子进程变为僵尸进程, # 僵尸进程虽然结束,但其PID仍然存在,该僵尸进程占用内存资源 #linux 终端查询僵尸进程命令 ps aux import os import time pid = os.fork() if pid < 0: print("error") elif pid == 0: print() print("child PID", os.getpid()) print("get parent PID", os.getppid()) else: time.sleep(2) print("parent PID", os.getpid()) print("get child PID", pid)
def run(): experts_etl_logger.info('starting: experts etl', extra={ 'pid': str(os.getpid()), 'ppid': str(os.getppid()), }) for module_name in extractor_loaders + transformer_loaders + syncers: try: # Must include a comma after a single arg, or multiprocessing seems to # get confused and think we're passing multiple arguments: p = mp.Process(target=subprocess, args=(module_name, )) p.start() p.join() except Exception as e: formatted_exception = loggers.format_exception(e) experts_etl_logger.error( f'attempt to execute {module_name} in a new process failed: {formatted_exception}', extra={ 'pid': str(os.getpid()), 'ppid': str(os.getppid()), }) # api_version_collections_map = {} # with db.cx_oracle_connection() as connection: # cursor = connection.cursor() # #for api_version in pure_json.api_versions(cursor): # Later! # for api_version in ['518']: # For now, until we create tables for other versions. # collections = pure_json.collection_api_names_for_api_version(cursor, api_version=api_version) # api_version_collections_map[api_version] = collections # # # Just 1 process for now, until we add other api_versions. But do we even need param? Defaults to os.cpu_count. # with mp.Pool(processes=1) as pool: # results = [] # for api_version in api_version_collections_map: # try: # results.append( # pool.apply_async(extract_load_changes, ({'api_version': api_version},)) # ) # except Exception as e: # formatted_exception = loggers.format_exception(e) # experts_etl_logger.error( # f'attempt to extract/load changes raw json in a new process failed: {formatted_exception}', # extra={ # 'pid': str(os.getpid()), # 'ppid': str(os.getppid()), # } # ) # for result in results: # result.get() # # with mp.Pool() as pool: # results = [] # for api_version, collection_api_names in api_version_collections_map.items(): # for collection_api_name in collection_api_names: # try: # results.append( # pool.apply_async(extract_load_collection, (collection_api_name, {'api_version': api_version},)) # ) # except Exception as e: # formatted_exception = loggers.format_exception(e) # experts_etl_logger.error( # f'attempt to extract/load {collection_api_name} raw json in a new process failed: {formatted_exception}', # extra={ # 'pid': str(os.getpid()), # 'ppid': str(os.getppid()), # } # ) # for result in results: # result.get() experts_etl_logger.info('ending: experts etl', extra={ 'pid': str(os.getpid()), 'ppid': str(os.getppid()), }) loggers.rollover(experts_etl_logger)
# coding=utf-8 import os import time ret = os.fork() # 有两个返回值 第一次返回主线程的 第二次返回子线程的 子线程永远返回0 print("ret的值是:%d 线程是:%d" % (ret, os.getpid())) if ret == 0: # 子线程 print("子线程是:%d" % os.getpid()) print("主线程是:%d" % os.getppid()) time.sleep(5) print("hahaha") else: # 主线程 print("线程是:%d" % os.getpid()) time.sleep(3) print("哈哈哈哈哈") print("---over---")
from os import getppid from psutil import Process import asyncio, requests, websockets import data_handler, config_handler, logger cfg = config_handler.__init__("settings.ini") log = logger.__init__() sl_token = cfg.get("Credentials", "sl_token") pb_token = cfg.get("Credentials", "pb_token") parent_id = getppid() parent_exe = Process(parent_id).name() log.info("{0} process id: {1}".format(parent_exe, parent_id)) async def notifications(): log.info("Connecting to Pushbullet WSS stream") async with websockets.connect( "wss://stream.pushbullet.com/websocket/{0}".format( pb_token)) as websocket: while True: data = await websocket.recv() if Process(parent_id).name() != parent_exe: log.debug("Parent process isn't available, closing self") raise SystemExit if "push" and "application_name" in data: name, msg, amount = data_handler.handleJSON().Payments(data)
def test_get_proc_ppid(self): """get_proc_ppid returns correct parent pid value.""" my_pid = os.getpid() my_ppid = os.getppid() self.assertEqual(my_ppid, util.get_proc_ppid(my_pid))
def run(self, *args, **kwargs): print(os.getpid()) print(os.getppid())
import os # 获取进程的id print(os.getpid()) # 获取父级进程id print(os.getppid()) # 创建子进程(只在类Linux 和 类unix 系统中,有fork(),相当于复制程序) # os.fork()
def run_proc(name): print('子进程运行 name=:%s 我的ID=:%s ,父级进程的ID=:%s' % (name, os.getpid(),os.getppid()))
def reader(q): print("reader启动(%s),父进程为%s"%(os.getpid(),os.getppid())) for i in range(q.qsize()): print("reader从Queue获取到消息:%s"%q.get(True))
def test(name): print("[子进程-%s]PID:%d,PPID:%d" % (name, os.getpid(), os.getppid()))
import os # 注意,fork函数,只在Unix/Linux/Mac上运行,Windows不可以 rpid = os.fork() if __name__ == '__main__': if rpid < 0: print("fork调用失败。") elif rpid == 0: print("我是子进程(%s),我的父进程是(%s)" % (os.getpid(), os.getppid())) # x += 1 else: print("我是父进程(%s),我的子进程是(%s)" % (os.getpid(), rpid)) print("父子进程都可以执行这的代码")
import os print("parent pid = " + str(os.getppid())) print("my pid = " + str(os.getpid()))
def create_logger(conf:Config): global logger logger.close() # close any previous instances conf_common = get_conf_common(conf) expdir = conf_common['expdir'] distdir = conf_common['distdir'] log_prefix = conf_common['log_prefix'] yaml_log = conf_common['yaml_log'] log_level = conf_common['log_level'] if utils.is_main_process(): logdir, log_suffix = expdir, '' else: logdir, log_suffix = distdir, '_' + str(os.getpid()) # ensure folders os.makedirs(logdir, exist_ok=True) # file where logger would log messages sys_log_filepath = utils.full_path(os.path.join(logdir, f'{log_prefix}{log_suffix}.log')) logs_yaml_filepath = utils.full_path(os.path.join(logdir, f'{log_prefix}{log_suffix}.yaml')) experiment_name = get_experiment_name(conf) + log_suffix #print(f'experiment_name={experiment_name}, log_stdout={sys_log_filepath}, log_file={sys_log_filepath}') sys_logger = utils.create_logger(filepath=sys_log_filepath, name=experiment_name, level=log_level, enable_stdout=True) if not sys_log_filepath: sys_logger.warn( 'log_prefix not specified, logs will be stdout only') # reset to new file path logger.reset(logs_yaml_filepath, sys_logger, yaml_log=yaml_log, backup_existing_file=False) logger.info({'command_line': ' '.join(sys.argv) if utils.is_main_process() else f'Child process: {utils.process_name()}-{os.getpid()}'}) logger.info({'process_name': utils.process_name(), 'is_main_process': utils.is_main_process(), 'main_process_pid':utils.main_process_pid(), 'pid':os.getpid(), 'ppid':os.getppid(), 'is_debugging': utils.is_debugging()}) logger.info({'experiment_name': experiment_name, 'datetime:': datetime.datetime.now()}) logger.info({'logs_yaml_filepath': logs_yaml_filepath, 'sys_log_filepath': sys_log_filepath})
def writer(q): print("writter启动(%s),父进程为%s"%(os.getpid(),os.getppid())) for i in "Doris": q.put(i)
def run_proc(name): print('子进程代码运行,子进程id: %s, 父进程id: %s,name:%s'%(os.getpid(),os.getppid(),name))
def main(): print("[父进程]PID:%d,PPID:%d" % (os.getpid(), os.getppid())) p = Process(target=test, args=("萌萌哒", )) p.start() p.join() # 父进程回收子进程资源(内部调用了wait系列方法)
import os pid = os.fork() if pid < 0: print('调用失败') elif pid == 0: print('子进程:%d,父进程:%d' % (os.getpid(), os.getppid())) else: print('父进程:%d,子进程:%d' % (os.getpid(), pid))
def seed(): return os.getppid()
import os from time import sleep pid = os.fork() if pid < 0: print("Error") elif pid == 0: sleep(1) print("Child PID", os.getpid()) # 获取父进程PID print("Get Parent PID", os.getppid()) else: print("Parent PID", os.getpid()) # 获取子进程PID print("Get Child PID", pid)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os print('Process (%s) start...' % os.getpid()) # Only works on Unix/Linux/Mac: pid = os.fork() if pid == 0: print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid())) else: print('I (%s) just created a child process (%s).' % (os.getpid(), pid))
def run(self): print('子进程%s开始执行,父进程为%s' % (os.getpid(), os.getppid())) t_start = time.time() time.sleep(self.interval) t_stop = time.time() print('%s执行结束,耗时%0.2f秒' % (os.getpid(), (t_stop - t_start)))