Forking python, defunct child

2024/11/17 18:31:17

I have some troubles with Python child processes so I wrote a very simple script:

import os
import sys
import timepid = os.fork()
if pid:#parenttime.sleep(30)
else:#child#os._exit(0)sys.exit()

While parent process is sleeping I launch

ps fax | grep py[t]hon

And I read this output

2577 ?        S      0:00 python /home/pi/python/GPIO/GPIODaemon.py restart
2583 ?        Z      0:00  \_ [python] <defunct>

Using sys.exit()oros._exit(0) there is always a Zombie process and I'm unable to understand why.

Working on my more complex code I was thinking that there was some resources that child processes were keeping locked, but on this simplified code child has no file/socket/db connection at all! Why is child process zombiefied?

Answer

To clear the child process in Unix you need to wait on the child, check one of the os.wait(), os.waitpid(), os.wait3() or os.wait4() at http://docs.python.org/2/library/os.html#os.wait

As to why this is so, this is a design decision of Unix. The child process keeps its return value in its process state, if it was to disappear you'll have no return value. The os.wait() also returns to you the return value and then the child process is released and all associated resources are released.

https://en.xdnf.cn/q/71157.html

Related Q&A

is there a way to know the length of a bytearray variable in python?

I have this code:variable = "FFFF" message = bytearray( variable.decode("hex") )after this, I want to perform something like this:message.len()but it seems that bytearray does not h…

Matplotlib: Check for empty plot

I have a loop which loads and plots some data, something like this:import os import numpy as np import matplotlib.pyplot as pltfor filename in filenames:plt.figure()if os.path.exists(filename):x, y = n…

Hyperlink in Streamlit dataframe

I am attempting to display a clickable hyperlink inside a dataframe containing filtered results on Streamlit. This is my code so far: import pandas as pd import streamlit as st import openpyxl import n…

Best way to detect if checkbox is ticked

My work:Scan the paper Check horizontal and vertical line Detect checkbox How to know checkbox is ticked or notAt this point, I thought I could find it by using Hierarchical and Contours: Below is my w…

ResultSet object has no attribute find_all

i always met one problem, when I scraping one web page.AttributeError: ResultSet object has no attribute find. Youre probably treating a list of items like a single item. Did you call find_all() when y…

How can I set path to load data from CSV file into PostgreSQL database in Docker container?

I would like to load data from CSV file into PostgreSQL database in Docker. I run:docker exec -ti my project_db_1 psql -U postgresThen I select my database:\c myDatabaseNow I try to load data from myfi…

Why is this simple Spark program not utlizing multiple cores?

So, Im running this simple program on a 16 core multicore system. I run it by issuing the following.spark-submit --master local[*] pi.pyAnd the code of that program is the following. #"""…

How to get python dictionaries into a pandas time series dataframe where key is date object

I have a python dictionaries where the key is a dateobject and the value is the timeseires.timeseries = {datetime.datetime(2013, 3, 17, 18, 19): {t2: 400, t1: 1000},datetime.datetime(2013, 3, 17, 18, 2…

Changing the color of an image based on RGB value

Situation:You have an image with 1 main color and you need to convert it to another based on a given rgb value.Problem:There are a number of different, but similar shades of that color that also need …

Python NumPy - FFT and Inverse FFT?

Ive been working with FFT, and Im currently trying to get a sound waveform from a file with FFT, (modify it eventually), but then output that modified waveform back to a file. Ive gotten the FFT of the…