Python Forum
How to background another process in Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to background another process in Python?
#1
The thing is, I'm trying to get a process inside my script and have me call it whenever I want. For example, I'm filling in some data and I enter the word "background". In this way, it is called a process that is always active. I leave my code in python, I have created a decorator but I get the following error: "EOFError".

Besides that, it should work both ways, when I'm in the background and insert "background", it should return to the main function.

I share with you the code I've been doing:

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


import os, time

def daemonLife(func):
    def wrapper(*args, **kwargs):
        if os.fork(): return
        func(*args, **kwargs)
        os._exit(os.EX_OK)
    return wrapper

@daemonLife
def my_func():
   no_kill_nothing = "Hi!"
   print('pid of it: %d' % os.getppid())
   var2 = str(input("Inside of background!"))
   if var2 == "background":
      main(no_kill_nothing)

def main(text):
   if text == "background":
      my_func()

text = str(input("Insert something"))
main(text)
I tried multi-threading because I think it's more ideal but I haven't been able to.

Thank you very much!
Reply
#2
Quote:Unless stdin is close-on-exec, the child should be able to read from it after the parent is terminated. What you are experiencing, however, is probably a conflict with the terminal's process group control and the shell's job control. When the parent dies, the shell takes back over as the foreground process of the terminal. Background processes trying to read from the terminal get a SIGTTIN. Is it explicitly requested to read from the terminal from the child process, because that seems simply a bad idea (why not keep the parent)?

I dont' sure about EOFError, you can read more at https://stackoverflow.com/questions/3479...nt-process
Reply
#3
(Oct-20-2019, 05:15 PM)Kalet Wrote: [ ... ] I get the following error: "EOFError".
[ ... ]

        if os.fork(): return

Hi!

Just a thought ... Your line 9:

        if os.fork(): return
shouldn't be like this?:

        if os.fork(): 
            return
As it seems the python tags eliminated the indentation, I'm going to use 'X' to mark the indentation.
So I was saying that your line 9:

XXXXXXXXif os.fork(): return

shouldn't be like this?:

XXXXXXXXif os.fork():
XXXXXXXXXXXXreturn


All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question about Creating an Automated Process in Python Supratik1234 0 741 Jan-13-2023, 08:29 PM
Last Post: Supratik1234
  Python multiprocessing Pool apply async wait for process to complete sunny9495 6 6,401 Apr-02-2022, 06:31 AM
Last Post: sunny9495
  Process the image on the Python HTTP server Aleks 0 3,198 Dec-02-2021, 11:43 PM
Last Post: Aleks
  How to to tie the execution of one process to another inside a loop in Python ignorant_wanderer 0 2,044 Jul-11-2020, 03:44 AM
Last Post: ignorant_wanderer
  win32 API: Launch Application to RDP session from background process python script rangeshgupta 0 2,138 May-28-2020, 09:41 PM
Last Post: rangeshgupta
  Spawning a new process that is not attached to python cman234 3 1,887 Apr-25-2020, 05:24 PM
Last Post: cman234
  autostart python scripts in background (Windows10) john36 4 7,685 Oct-01-2019, 01:36 PM
Last Post: john36
  How to sharing object between multiple process from main process using Pipe Subrata 1 3,655 Sep-03-2019, 09:49 PM
Last Post: woooee
  changing the process command line arguments in python Skaperen 3 3,003 Aug-19-2019, 02:40 PM
Last Post: wavic
  random behavriour when handle process termination signal in Python hamzeah 2 2,032 Jul-31-2019, 07:32 AM
Last Post: hamzeah

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020