-1

I want to check whether thread will be preserved during os.fork. This is what the thread is started before os.fork.

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

import os
import multiprocessing
from threading import Timer
import time

i = [0]

def work():
    while True:
        time.sleep(1)
        i[0] += 1
        print i[0]
        print multiprocessing.current_process()


if __name__ == '__main__':
    task = Timer(1, work)
    task.daemon = True
    task.start()
    time.sleep(2)
    print 'pre fork'
    if os.fork():
        # parent process
    else:
        # child process
        multiprocessing.current_process().name = "Secondary"
    time.sleep(2)
    print 'post fork'
    time.sleep(1000)

The output of this program:

pre fork
1
<_MainProcess(MainProcess, started)>
2
<_MainProcess(MainProcess, started)>
post fork
post fork
3
<_MainProcess(MainProcess, started)>
4
<_MainProcess(MainProcess, started)>
5
<_MainProcess(MainProcess, started)>
6
<_MainProcess(MainProcess, started)>
7
<_MainProcess(MainProcess, started)>

==========================================================================

This is what the thread is started after os.fork.

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

import os
import multiprocessing
from threading import Timer
import time

i = [0]

def work():
    while True:
        time.sleep(1)
        i[0] += 1
        print i[0]
        print multiprocessing.current_process()


if __name__ == '__main__':
    task = Timer(1, work)
    task.daemon = True
    time.sleep(2)
    print 'pre fork'
    if os.fork():
        # parent process
        task.start()
    else:
        # sub process
        multiprocessing.current_process().name = "Secondary"
        task.start()
    time.sleep(2)
    print 'post fork'
    time.sleep(1000)

The output is like this:

pre fork
post fork
post fork
1
1
<_MainProcess(Secondary, started)>
<_MainProcess(MainProcess, started)>
2
2
<_MainProcess(MainProcess, started)>
<_MainProcess(Secondary, started)>

Summary

  • It seems that when we start the thread after os.fork then every thing will be ok and both the parent and child process will run the thread.
  • When we start the thread before os.fork, then according to the output, it seems that the thread is not come up with os.fork, i.e., it is not executed in the child process.

The question is how should this happen and what is the reason for it.

andy
  • 3,951
  • 9
  • 29
  • 40

1 Answers1

0

Suddenly, i come up with an explanation and the answer is No, thread will no be preserved during os.fork.

  • os.fork only do a copy-on-write.
  • parent and child process both have the same code.
  • However, if we start a thread before os.fork, then there no start logic to child process.
  • So, if we want a thread exist in child process then we can only start it after we have os.fork it.

:)

Note: This has some similar with gunicorn --preload argument.

andy
  • 3,951
  • 9
  • 29
  • 40