把当前的Python程序带到后台

把当前的Python程序带到后台,第1张

概述把当前的Python程序带到后台

一个python脚本打印信息到屏幕,然后应该去背景。

如何从脚本内部做到这一点?

Google云端硬盘作为服务或后台 *** 作

find由nohup命令运行的进程

windows编程DialogBox背景图片

透明背景JFrame linux上的animation

windows 10的uwp粘滞后台服务?

更新 :要正确地守护进程,请使用守护进程。

原始答案 :因为shell正在等待你的进程完成,所以主进程必须终止。 你可以使用os.fork() sys.exit()从父进程中分离一个子进程和sys.exit() :

import os import sys if os.fork(): sys.exit() # Child code goes here

复制相关代码以Python方式创建守护进程 ; 请阅读关于为什么代码是相当深思熟虑的意见。

def createDaemon(): """Detach a process from the controlling terminal and run it in the background as a daemon. """ try: # Fork a child process so the parent can exit. This returns control to # the command-line or shell. It also guarantees that the child will not # be a process group leader,since the child receives a new process ID # and inherits the parent's process group ID. This step is required # to insure that the next call to os.setsID is successful. pID = os.fork() except OSError,e: raise Exception,"%s [%d]" % (e.strerror,e.errno) if (pID == 0): # The first child. # To become the session leader of this new session and the process group # leader of the new process group,we call os.setsID(). The process is # also guaranteed not to have a controlling terminal. os.setsID() # Is ignoring SIGHUP necessary? # # It's often suggested that the SIGHUP signal should be ignored before # the second fork to avoID premature termination of the process. The # reason is that when the first child terminates,all processes,eg # the second child,in the orphaned group will be sent a SIGHUP. # # "However,as part of the session management system,there are exactly # two cases where SIGHUP is sent on the death of a process: # # 1) When the process that dIEs is the session leader of a session that # is attached to a terminal device,SIGHUP is sent to all processes # in the foreground process group of that terminal device. # 2) When the death of a process causes a process group to become # orphaned,and one or more processes in the orphaned group are # stopped,then SIGHUP and SIGCONT are sent to all members of the # orphaned group." [2] # # The first case can be ignored since the child is guaranteed not to have # a controlling terminal. The second case isn't so easy to dismiss. # The process group is orphaned when the first child terminates and # POSIX.1 requires that every StopPED process in an orphaned process # group be sent a SIGHUP signal followed by a SIGCONT signal. Since the # second child is not StopPED though,we can safely forego ignoring the # SIGHUP signal. In any case,there are no ill-effects if it is ignored. # # import signal # Set handlers for asynchronousous events. # signal.signal(signal.SIGHUP,signal.SIG_IGN) try: # Fork a second child and exit immediately to prevent zombIEs. This # causes the second child process to be orphaned,making the init # process responsible for its cleanup. And,since the first child is # a session leader without a controlling terminal,it's possible for # it to acquire one by opening a terminal in the future (System V- # based systems). This second fork guarantees that the child is no # longer a session leader,preventing the daemon from ever acquiring # a controlling terminal. pID = os.fork() # Fork a second child. except OSError,e.errno) if (pID == 0): # The second child. # Since the current working directory may be a mounted filesystem,we # avoID the issue of not being able to unmount the filesystem at # shutdown time by changing it to the root directory. os.chdir(workdir) # We probably don't want the file mode creation mask inherited from # the parent,so we give the child complete control over permissions. os.umask(UMASK) else: # exit() or _exit()? See below. os._exit(0) # Exit parent (the first child) of the second child. else: # exit() or _exit()? # _exit is like exit(),but it doesn't call any functions registered # with atexit (and on_exit) or any registered signal handlers. It also # closes any open file descriptors. Using exit() may cause all stdio # streams to be flushed twice and any temporary files may be unexpectedly # removed. It's therefore recommended that child branches of a fork() # and the parent branch(es) of a daemon use _exit(). os._exit(0) # Exit parent of the first child.

总结

以上是内存溢出为你收集整理的把当前的Python程序带到后台全部内容,希望文章能够帮你解决把当前的Python程序带到后台所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1228770.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存