Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8867679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:06:11+00:00 2026-06-14T17:06:11+00:00

I have a rather large database import job that I start from a web

  • 0

I have a rather large database import job that I start from a web interface (Flask). When visiting the URL that trigger the database import, I fork before doing database inserts.

The problem is that when I attempt to stop the web service (Ctrl+C on the built in development server) the child process stops instead, and the web server keep running in the background. Somehow it appears as if the web server job is suddenly pushed to becoming a background daemon while the child process now is the primary process.

What I want to achieve is a “start child process and then forget about it” approach where the web server just starts the child process and then never bother about it again, especially when it comes to exceptions and similar.

Any ideas on how this is best solved?

Currently the test code for the import function is:

def import_start():
    try:
            pid = os.fork()
    except OSError as e:
            print "Exception in import_start"
            sys.exit(1)
    if pid == 0:
          with open("/tmp/web_out.txt", "w") as f:
                for x in range(100):
                      f.write("line %d\n" % (x))
                      f.flush()
                      sleep(10)

The code is started in a flask route handler with:

import_start()

Using Ctrl+C afterwards will kill the import_start() process instead of the web server. I want it to be the other way around as the two processes should be completely independent of each other after launch.

UPDATE:

I ended up doing:

def start_import():
            subprocess.Popen([sys.executable,__file__],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

def do_import():
    with open("/tmp/web_out.txt", "w") as f:
            for x in range(100):
                    f.write("line %d\n" % (x))
                    f.flush()
                    sleep(10)

if __name__ == "__main__":
    do_import()

This works (except for killing the spawned process when my web server is killed), but I am a bit worried about not cleaning up the process somehow. I might look into the MQ approach to solve this, although this requires more complexity than just spawning a new process.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:06:11+00:00Added an answer on June 14, 2026 at 5:06 pm

    Maybe python-daemon could help. This is mainly used to start a python program as daemon. Afaik it does the double fork magic and detaches the process from the current one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rather large database that has alot of decimal columns in alot
I'm about to start a new project which should have a rather large database.
I have rather large project that uses ICU regex classes. Basically it might run
I have a rather large amount of data (100 MB or so), that I
I have a rather large application that has literally a hundred DDLs with Yes
I have data database containing some rather large strings, each of which holds a
Here is my dilema, I have a rather large PHP script, that basically reads
I have inherited a (rather large) database, which at the moment is only ever
Hello, stackoverflow community! I am working on a rather large database-driven web application. The
How do I pull a spring bean manually? I have a rather large web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.