python multiprocessing模块进程damon

Python中关于线程有daemon这个选项,如果不设置,主线程退出,子线程被系统接管一直运行到结束,进程也是一样的,但是multiprocessing中的Pool却无法设置
multiprocessing的文档:

daemon

The process’s daemon flag, a Boolean value. This must be set before start() is called.

The initial value is inherited from the creating process.

When a process exits, it attempts to terminate all of its daemonic child processes.

Note that a daemonic process is not allowed to create child processes. Otherwise a daemonic process would leave its children
orphaned if it gets terminated when its parent process exits.
Additionally, these are not Unix daemons or services, they are normal
processes that will be terminated (and not joined) if non-daemonic
processes have exited.

由于multiprocessing.Pool必须创建工作进程,因此您无法使用它来进行daemonaize进程.
但是Process可以设置,我们通过程序也可以知道Pool实例对象对象的确没这个方法
代码如下:

from multiprocessing import Pool,Process
import  os,time,threading
from datetime import  datetime
def fun(*args,**kwargs):
    print(f"线程名称:{threading.current_thread().getName()},线程ID:{threading.get_ident()},进程ID:{os.getpid()},父进程ID:{os.getppid()}")
    time.sleep(5)
    print(f"args value is:{args},kwargs value is:{kwargs}")
    print(f"now is {datetime.now()}")

if __name__=='__main__':
    pool=Pool(5)
    print(dir(pool))
    for i in range(11):
        pool.apply_async(fun,(i,),kwds={'i':i})
    pool.close()
    pool.join()
    print(f"结束运行,进程ID:{os.getpid()},父进程:{os.getppid()}")
(python38) [root@mysql04 Apschedule]# python views.py 
['Process', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_cache', '_change_notifier', '_check_running', '_ctx', '_get_sentinels', '_get_tasks', '_get_worker_sentinels', '_guarded_task_generation', '_handle_results', '_handle_tasks', '_handle_workers', '_help_stuff_finish', '_initargs', '_initializer', '_inqueue', '_join_exited_workers', '_maintain_pool', '_map_async', '_maxtasksperchild', '_outqueue', '_pool', '_processes', '_quick_get', '_quick_put', '_repopulate_pool', '_repopulate_pool_static', '_result_handler', '_setup_queues', '_state', '_task_handler', '_taskqueue', '_terminate', '_terminate_pool', '_wait_for_updates', '_worker_handler', '_wrap_exception', 'apply', 'apply_async', 'close', 'imap', 'imap_unordered', 'join', 'map', 'map_async', 'starmap', 'starmap_async', 'terminate']
线程名称:MainThread,线程ID:139908950198080,进程ID:3124,父进程ID:3122
线程名称:MainThread,线程ID:139908950198080,进程ID:3125,父进程ID:3122
线程名称:MainThread,线程ID:139908950198080,进程ID:3126,父进程ID:3122
线程名称:MainThread,线程ID:139908950198080,进程ID:3127,父进程ID:3122
线程名称:MainThread,线程ID:139908950198080,进程ID:3123,父进程ID:3122
args value is:(0,),kwargs value is:{'i': 0}
now is 2022-06-15 11:55:31.540195
线程名称:MainThread,线程ID:139908950198080,进程ID:3124,父进程ID:3122
args value is:(1,),kwargs value is:{'i': 1}
now is 2022-06-15 11:55:31.540440
线程名称:MainThread,线程ID:139908950198080,进程ID:3125,父进程ID:3122
args value is:(2,),kwargs value is:{'i': 2}
now is 2022-06-15 11:55:31.540600
线程名称:MainThread,线程ID:139908950198080,进程ID:3126,父进程ID:3122
args value is:(3,),kwargs value is:{'i': 3}
now is 2022-06-15 11:55:31.540757
线程名称:MainThread,线程ID:139908950198080,进程ID:3127,父进程ID:3122
args value is:(4,),kwargs value is:{'i': 4}
now is 2022-06-15 11:55:31.540910
线程名称:MainThread,线程ID:139908950198080,进程ID:3123,父进程ID:3122
args value is:(5,),kwargs value is:{'i': 5}
now is 2022-06-15 11:55:36.545561
线程名称:MainThread,线程ID:139908950198080,进程ID:3124,父进程ID:3122
args value is:(6,),kwargs value is:{'i': 6}
now is 2022-06-15 11:55:36.545728
args value is:(7,),kwargs value is:{'i': 7}
now is 2022-06-15 11:55:36.545810
args value is:(8,),kwargs value is:{'i': 8}
now is 2022-06-15 11:55:36.545880
args value is:(9,),kwargs value is:{'i': 9}
now is 2022-06-15 11:55:36.545958
args value is:(10,),kwargs value is:{'i': 10}
now is 2022-06-15 11:55:41.550771
结束运行,进程ID:3122,父进程:2677

dir打印出来的方法的确有这个方法
下面使用Process

from multiprocessing import Pool,Process
import  os,time,threading
from datetime import  datetime
def fun(*args,**kwargs):
    print(f"线程名称:{threading.current_thread().getName()},线程ID:{threading.get_ident()},进程ID:{os.getpid()},父进程ID:{os.getppid()}")
    time.sleep(5)
    print(f"args value is:{args},kwargs value is:{kwargs}")
    print(f"now is {datetime.now()}")

if __name__=='__main__':
    for i in range(5):
        p=Process(target=fun,args=(i,),kwargs=dict(i=i))
        # p.daemon=True
        p.start()
        print(p.daemon)

运行结果:

(python38) [root@mysql04 Apschedule]# python views.py 
False
False
False
False
False
结束运行,进程ID:3167,父进程:2677
线程名称:MainThread,线程ID:140190060099392,进程ID:3172,父进程ID:3167
线程名称:MainThread,线程ID:140190060099392,进程ID:3169,父进程ID:3167
线程名称:MainThread,线程ID:140190060099392,进程ID:3170,父进程ID:3167
线程名称:MainThread,线程ID:140190060099392,进程ID:3171,父进程ID:3167
线程名称:MainThread,线程ID:140190060099392,进程ID:3168,父进程ID:3167
args value is:(4,),kwargs value is:{'i': 4}
now is 2022-06-15 11:57:56.406548
args value is:(1,),kwargs value is:{'i': 1}
now is 2022-06-15 11:57:56.412359
args value is:(2,),kwargs value is:{'i': 2}
now is 2022-06-15 11:57:56.412651
args value is:(3,),kwargs value is:{'i': 3}
now is 2022-06-15 11:57:56.412905
args value is:(0,),kwargs value is:{'i': 0}
now is 2022-06-15 11:57:56.413150

如果加上daemon=True

from multiprocessing import Pool,Process
import  os,time,threading
from datetime import  datetime
def fun(*args,**kwargs):
    print(f"线程名称:{threading.current_thread().getName()},线程ID:{threading.get_ident()},进程ID:{os.getpid()},父进程ID:{os.getppid()}")
    time.sleep(5)
    print(f"args value is:{args},kwargs value is:{kwargs}")
    print(f"now is {datetime.now()}")

if __name__=='__main__':
    for i in range(5):
        p=Process(target=fun,args=(i,),kwargs=dict(i=i))
        p.daemon=True
        p.start()
        print(p.daemon)
    print(f"结束运行,进程ID:{os.getpid()},父进程:{os.getppid()}")
(python38) [root@mysql04 Apschedule]# python views.py 
True
True
True
True
True
结束运行,进程ID:3201,父进程:2677

直接就运行结束了,子进程随着父进程一块结束
上面运行程序看Pool和Process都是主进程的子进程,从ppid可以得出
Process可以设置daemon=True

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值