def maybe_promote_master(self):
if self.master_pid == 0:
return
if self.master_pid != os.getppid():
self.log.info("Master has been promoted.")
# reset master infos
self.master_name = "Master"
self.master_pid = 0
self.proc_name = self.cfg.proc_name
del os.environ['GUNICORN_PID']
# rename the pidfile
if self.pidfile is not None:
self.pidfile.rename(self.cfg.pidfile)
# reset proctitle
util._setproctitle("master [%s]" % self.proc_name)
python类getppid()的实例源码
def test_garbage_collect_2(self):
"""
Garbage collection should not remove live pid.
"""
self.args.session = os.getppid()
working_dir = common.get_working_dir(self.args)
assert os.path.exists(working_dir)
common.garbage_collect()
assert os.path.exists(working_dir)
def test_set_state(self):
self.args.session = os.getppid()
state = {
"connections": {
"foo": {
"url": "some url"}}}
common.set_state(self.args, state)
result = common.get_state(self.args)
assert state == result
def _daemonize(self):
# double-fork. refer to "Advanced Programming in the UNIX Environment"
try: # first fork
pid = os.fork()
if pid > 0: # first parent
os.waitpid(pid, 0) # wait for second child to start
return False # return to caller of daemonize()
except OSError, e:
self.log('fork #1 failed: %s' % e)
return # return caller of daemonize()
# decouple first parent
os.setsid()
os.chdir("/")
os.umask(0)
ppid = os.getpid() # yes, getpid(). it will be the child's ppid
try: # second fork
self._pid = os.fork()
if self._pid > 0: # second parent. just exit
os._exit(0) # this is the wait() above
except OSError, e:
self.log('fork #2 failed: %s' % e)
os._exit(1)
# wait until ppid changes
while os.getppid() == ppid:
time.sleep(0.1)
return True
def _daemonize(self):
# double-fork. refer to "Advanced Programming in the UNIX Environment"
try: # first fork
pid = os.fork()
if pid > 0: # first parent
os.waitpid(pid, 0) # wait for second child to start
return False # return to caller of daemonize()
except OSError, e:
self.log('fork #1 failed: %s' % e)
return # return caller of daemonize()
# decouple first parent
os.setsid()
os.chdir("/")
os.umask(0)
ppid = os.getpid() # yes, getpid(). it will be the child's ppid
try: # second fork
self._pid = os.fork()
if self._pid > 0: # second parent. just exit
os._exit(0) # this is the wait() above
except OSError, e:
self.log('fork #2 failed: %s' % e)
os._exit(1)
# wait until ppid changes
while os.getppid() == ppid:
time.sleep(0.1)
return True
def _process_entry_point(channel, iface_name):
logger.info('Bus monitor process started with PID %r', os.getpid())
app = QApplication(sys.argv) # Inheriting args from the parent process
def exit_if_should():
if RUNNING_ON_WINDOWS:
return False
else:
return os.getppid() != PARENT_PID # Parent is dead
exit_check_timer = QTimer()
exit_check_timer.setSingleShot(False)
exit_check_timer.timeout.connect(exit_if_should)
exit_check_timer.start(2000)
def get_frame():
received, obj = channel.receive_nonblocking()
if received:
if obj == IPC_COMMAND_STOP:
logger.info('Bus monitor process has received a stop request, goodbye')
app.exit(0)
else:
return obj
win = BusMonitorWindow(get_frame, iface_name)
win.show()
logger.info('Bus monitor process %r initialized successfully, now starting the event loop', os.getpid())
sys.exit(app.exec_())
# TODO: Duplicates PlotterManager; refactor into an abstract process factory
def _process_entry_point(channel):
logger.info('Plotter process started with PID %r', os.getpid())
app = QApplication(sys.argv) # Inheriting args from the parent process
def exit_if_should():
if RUNNING_ON_WINDOWS:
return False
else:
return os.getppid() != PARENT_PID # Parent is dead
exit_check_timer = QTimer()
exit_check_timer.setSingleShot(False)
exit_check_timer.timeout.connect(exit_if_should)
exit_check_timer.start(2000)
def get_transfer():
received, obj = channel.receive_nonblocking()
if received:
if obj == IPC_COMMAND_STOP:
logger.info('Plotter process has received a stop request, goodbye')
app.exit(0)
else:
return obj
win = PlotterWindow(get_transfer)
win.show()
logger.info('Plotter process %r initialized successfully, now starting the event loop', os.getpid())
sys.exit(app.exec_())
def daemon_start(pid_file, log_file):
def handle_exit(signum, _):
if signum == signal.SIGTERM:
sys.exit(0)
sys.exit(1)
signal.signal(signal.SIGINT, handle_exit)
signal.signal(signal.SIGTERM, handle_exit)
# fork only once because we are sure parent will exit
pid = os.fork()
assert pid != -1
if pid > 0:
# parent waits for its child
time.sleep(5)
sys.exit(0)
# child signals its parent to exit
ppid = os.getppid()
pid = os.getpid()
if write_pid_file(pid_file, pid) != 0:
os.kill(ppid, signal.SIGINT)
sys.exit(1)
os.setsid()
signal.signal(signal.SIG_IGN, signal.SIGHUP)
print('started')
os.kill(ppid, signal.SIGTERM)
sys.stdin.close()
try:
freopen(log_file, 'a', sys.stdout)
freopen(log_file, 'a', sys.stderr)
except IOError as e:
shell.print_exception(e)
sys.exit(1)
def daemon_start(pid_file, log_file):
def handle_exit(signum, _):
if signum == signal.SIGTERM:
sys.exit(0)
sys.exit(1)
signal.signal(signal.SIGINT, handle_exit)
signal.signal(signal.SIGTERM, handle_exit)
# fork only once because we are sure parent will exit
pid = os.fork()
assert pid != -1
if pid > 0:
# parent waits for its child
time.sleep(5)
sys.exit(0)
# child signals its parent to exit
ppid = os.getppid()
pid = os.getpid()
if write_pid_file(pid_file, pid) != 0:
os.kill(ppid, signal.SIGINT)
sys.exit(1)
os.setsid()
signal.signal(signal.SIG_IGN, signal.SIGHUP)
print('started')
os.kill(ppid, signal.SIGTERM)
sys.stdin.close()
try:
freopen(log_file, 'a', sys.stdout)
freopen(log_file, 'a', sys.stderr)
except IOError as e:
shell.print_exception(e)
sys.exit(1)
def parent_is_alive(self):
if os.getppid() == 1:
logger.info("Parent process has gone away, exiting process {}!".format(os.getpid()))
return False
return True
def fork_case():
#can't work under windows
print "Process %s start " %os.getpid()
pid=os.fork()
if pid==0:
print "i am child process %s. and my parent pid is %s " %(os.getpid(),os.getppid())
else:
print "i am parent process %s, and create my child process %s" %(os.getpid(),pid)
def basic_usage():
print 'pid ',os.getpid()
#print 'ppid ',os.getppid()
cpus = multiprocessing.cpu_count()
print cpus
name=multiprocessing.current_process().name
print name
Python multiprocessing example- Process.py 文件源码
项目:Parallel-Processing-Nadig
作者: madhug-nadig
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def process_info():
print('Module:' + str(__name__) + '\n')
print('Parent Process id:' + str(os.getppid())+ '\n' )
print('Process id:' + str(os.getpid())+ '\n\n' )
Python multiprocessing example- Pools.py 文件源码
项目:Parallel-Processing-Nadig
作者: madhug-nadig
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def process_info():
print('Module:' + str(__name__) + '\n')
print('Parent Process id:' + str(os.getppid())+ '\n' )
print('Process id:' + str(os.getpid())+ '\n\n' )
def start_or_get_daemon(self):
global daemon
if (daemon != None):
return daemon
is_windows = os.name == 'nt'
executable = 'importjsd'
try:
daemon = subprocess.Popen(
[executable, 'start', '--parent-pid', str(os.getppid())],
cwd=self.project_root(),
env=import_js_environment,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=is_windows
)
# The daemon process will print one line at startup of the command,
# something like "DAEMON active. Logs will go to [...]". We need to
# ignore this line so that we can expect json output when running
# commands.
daemon.stdout.readline()
return daemon
except FileNotFoundError as e:
if(e.strerror.find(executable) > -1):
# If the executable is in the error message, then we believe
# that the executable cannot be found and show a more helpful
# message.
sublime.error_message(no_executable_error(executable))
else:
# Something other than the executable cannot be found, so we
# pass through the original message.
sublime.error_message(e.strerror)
raise e
def watchdog(self):
if self.alive:
self.notify()
if self.ppid != os.getppid():
self.log.info("Parent changed, shutting down: %s", self)
self.alive = False
def is_parent_alive(self):
# If our parent changed then we shut down.
if self.ppid != os.getppid():
self.log.info("Parent changed, shutting down: %s", self)
return False
return True
def is_parent_alive(self):
# If our parent changed then we shut down.
if self.ppid != os.getppid():
self.log.info("Parent changed, shutting down: %s", self)
return False
return True
def _run(self):
for sock in self.sockets:
factory = self.get_factory(sock.sock, sock.cfg_addr)
self.servers.append(
(yield from self._create_server(factory, sock)))
# If our parent changed then we shut down.
pid = os.getpid()
try:
while self.alive or self.connections:
self.notify()
if (self.alive and
pid == os.getpid() and self.ppid != os.getppid()):
self.log.info("Parent changed, shutting down: %s", self)
self.alive = False
# stop accepting requests
if not self.alive:
if self.servers:
self.log.info(
"Stopping server: %s, connections: %s",
pid, len(self.connections))
for server in self.servers:
server.close()
self.servers.clear()
# prepare connections for closing
for conn in self.connections.values():
if hasattr(conn, 'closing'):
conn.closing()
yield from asyncio.sleep(1.0, loop=self.loop)
except KeyboardInterrupt:
pass
if self.servers:
for server in self.servers:
server.close()
yield from self.close()
def notify(self):
super(GeventWorker, self).notify()
if self.ppid != os.getppid():
self.log.info("Parent changed, shutting down: %s", self)
sys.exit(0)