Rodrigo Formighieri
Rodrigo Formighieri

Reputation: 477

Fork() from inside a spawned Thread in Python issue

I'am having problemes to kill the child processes from a fork spawn inside an also spawned thread:

_td = threading.Thread(target=updateProxies,args=())
_td.start()

def updateProxies():
    quota = 25
    children = []
    sons = 0
    for i in range(50):
        pid = os.fork()
        if pid:
            children.append(pid)
            sons+=1
            if sons >= quota:
                os.wait()
                sons-=1
        else:
             {CHILD CODE EXECUTION} #database calls, and network requests
             sys.exit()
    for x in children:
        os.waitpid(x,0)

When I run the code above, the parent from the children stops at the "os.waitpid(x,0)" line, and never resumes from there. And yes, I tracked all the children until they die at their respectively sys.exit(), but waitpid never gets informed about their death and my parent process never resumes! When doing ps -ef, the childs processes are (defunct) aren't they diyng?

IMPORTANT: when I execute the function from the main thread, everything goes fine. How to deal with it?

Upvotes: 2

Views: 758

Answers (1)

Rodrigo Formighieri
Rodrigo Formighieri

Reputation: 477

FOUND THE ANSWER: Had to exit the fork processes with:

os._exit(0)

not with

sys.exit()

Upvotes: 3

Related Questions