如何在Python中设置另一个进程的背景?

2024-10-01 00:16:10 发布

您现在位置:Python中文网/ 问答频道 /正文

问题是,我试图在我的脚本中加入一个进程,让我随时调用它。例如,我在填写一些数据,然后输入“background”一词。这样,它就被称为始终处于活动状态的进程。我把代码留在python中,我创建了一个decorator,但是我得到了以下错误:“eoferor”

除此之外,它应该双向工作,当我在后台并插入“background”时,它应该返回到main函数

我和大家分享我一直在做的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os, time

def daemonLife(func):
    def wrapper(*args, **kwargs):
        if os.fork(): return
        func(*args, **kwargs)
        os._exit(os.EX_OK)
    return wrapper

@daemonLife
def my_func():
   no_kill_nothing = "Hi!"
   print('pid of it: %d' % os.getppid())
   var2 = str(input("Inside of background!"))
   if var2 == "background":
      main(no_kill_nothing)

def main(text):
   if text == "background":
      my_func()

text = str(input("Insert something"))
main(text)

非常感谢


Tags: 代码textreturnif进程osmainmy