Python os.setpgid() Examples

The following are code examples for showing how to use os.setpgid(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: Comparative-Annotation-Toolkit   Author: ComparativeGenomicsToolkit   File: pipeline.py    (license) View Source Project 6 votes vote down vote up
def _setPgid(pid, pgid):
    """set pgid of a process, ignored exception caused by race condition
    that occurs if already set by parent or child has already existed"""
    # Should just ignore on EACCES, as to handle race condition with parent
    # and child.  However some Linux kernels (seen in 2.6.18-53) report ESRCH
    # or EPERM.  To handle this is a straight-forward way, just check that the
    # change has been made.  However, in some cases the change didn't take,
    # retrying seems to make the problem go away.
    for i in xrange(0,5):
        try:
            os.setpgid(pid, pgid)
            return
        except OSError:
            if os.getpgid(pid) == pgid:
                return
            time.sleep(0.25) # sleep for retry
    # last try, let it return an error
    os.setpgid(pid, pgid)

# FIXME: why not use pipes.quote? 
Example 2
Project: TikZ   Author: ellisk42   File: timeshare.py    (license) View Source Project 6 votes vote down vote up
def execute(self,dt):
        if self.finished: return "finished"
        if not self.running:
            self.process = Process(target = executeInProcessGroup, args = (self,))
            self.process.start()
            print "timeshare child PID:",self.process.pid
            os.setpgid(self.process.pid,self.process.pid)
            print "timeshare process group",os.getpgid(self.process.pid)
            assert os.getpgid(self.process.pid) == self.process.pid
            print "my process group",os.getpgrp(),"which should be",os.getpgid(0)
            assert os.getpgid(self.process.pid) != os.getpgid(0)
            self.running = True
        else:
            os.killpg(self.process.pid, signal.SIGCONT)
        
        self.process.join(dt)
        if self.process.is_alive():
            os.killpg(self.process.pid, signal.SIGSTOP)
            return "still running"
        else:
            self.finished = True
            return self.q.get() 
Example 3
Project: slug   Author: xonsh   File: posix.py    (license) View Source Project 6 votes vote down vote up
def start(self):
        """
        Start the process.
        """
        preexec = None
        if hasattr(self, '_process_group_leader'):
            # This probably needs some kind of syncronization...
            if self._process_group_leader is ...:
                preexec = os.setpgrp
            else:
                pgid = self._process_group_leader.pid

                def preexec():
                    os.setpgid(0, pgid)

        self._proc = subprocess.Popen(
            # What to execute
            self.cmd,
            preexec_fn=preexec,
            # What IO it has
            stdin=self.stdin, stdout=self.stdout, stderr=self.stderr,
            # Environment it executes in
            cwd=self.cwd, env=self.environ,
        ) 
Example 4
Project: bilean   Author: openstack   File: wsgi.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, name, conf, threads=1000):
        os.umask(0o27)  # ensure files are created with the correct privileges
        self._logger = logging.getLogger("eventlet.wsgi.server")
        self._wsgi_logger = WritableLogger(self._logger)
        self.name = name
        self.threads = threads
        self.children = set()
        self.stale_children = set()
        self.running = True
        self.pgid = os.getpid()
        self.conf = conf
        try:
            os.setpgid(self.pgid, self.pgid)
        except OSError:
            self.pgid = 0 
Example 5
Project: jupyter-powershell   Author: vors   File: killableprocess.py    (license) View Source Project 5 votes vote down vote up
def setpgid_preexec_fn():
    os.setpgid(0, 0) 
Example 6
Project: TikZ   Author: ellisk42   File: timeshare.py    (license) View Source Project 5 votes vote down vote up
def executeInProcessGroup(task):
    os.setpgid(0,0)
    task.q.put(task.command(*task.arguments)) 
Example 7
Project: locuszoom-standalone   Author: statgen   File: killableprocess.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, *args, **kwargs):
            if len(args) >= 7:
                raise Exception("Arguments preexec_fn and after must be passed by keyword.")

            real_preexec_fn = kwargs.pop("preexec_fn", None)
            def setpgid_preexec_fn():
                os.setpgid(0, 0)
                if real_preexec_fn:
                    apply(real_preexec_fn)

            kwargs['preexec_fn'] = setpgid_preexec_fn

            subprocess.Popen.__init__(self, *args, **kwargs) 
Example 8
Project: glare   Author: openstack   File: wsgi.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, threads=1000, initialize_glance_store=False):
        os.umask(0o27)  # ensure files are created with the correct privileges
        self._logger = logging.getLogger("eventlet.wsgi.server")
        self.threads = threads
        self.children = set()
        self.stale_children = set()
        self.running = True
        self.initialize_glance_store = initialize_glance_store
        self.pgid = os.getpid()
        try:
            os.setpgid(self.pgid, self.pgid)
        except OSError:
            self.pgid = 0 
Example 9
Project: slug   Author: xonsh   File: posix.py    (license) View Source Project 5 votes vote down vote up
def add(self, proc):
        super().add(proc)
        if self.started and proc.started and not isinstance(proc, base.VirtualProcess):
            os.setpgid(proc.pid, self.pgid) 
Example 10
Project: Flask_Blog   Author: sugarguo   File: preforkserver.py    (GNU General Public License v3.0) View Source Project 4 votes vote down vote up
def _spawnChild(self, sock):
        """
        Spawn a single child. Returns True if successful, False otherwise.
        """
        # This socket pair is used for very simple communication between
        # the parent and its children.
        parent, child = socket.socketpair()
        parent.setblocking(0)
        setCloseOnExec(parent)
        child.setblocking(0)
        setCloseOnExec(child)
        try:
            pid = os.fork()
        except OSError, e:
            if e[0] in (errno.EAGAIN, errno.ENOMEM):
                return False # Can't fork anymore.
            raise
        if not pid:
            # Child
            child.close()
            # Put child into its own process group.
            pid = os.getpid()
            os.setpgid(pid, pid)
            # Restore signal handlers.
            self._restoreSignalHandlers()
            # Close copies of child sockets.
            for f in [x['file'] for x in self._children.values()
                      if x['file'] is not None]:
                f.close()
            self._children = {}
            try:
                # Enter main loop.
                self._child(sock, parent)
            except KeyboardInterrupt:
                pass
            sys.exit(0)
        else:
            # Parent
            parent.close()
            d = self._children[pid] = {}
            d['file'] = child
            d['avail'] = True
            return True 
Example 11
Project: tissuelab   Author: VirtualPlants   File: preforkserver.py    (license) View Source Project 4 votes vote down vote up
def _spawnChild(self, sock):
        """
        Spawn a single child. Returns True if successful, False otherwise.
        """
        # This socket pair is used for very simple communication between
        # the parent and its children.
        parent, child = socket.socketpair()
        parent.setblocking(0)
        setCloseOnExec(parent)
        child.setblocking(0)
        setCloseOnExec(child)
        try:
            pid = os.fork()
        except OSError, e:
            if e[0] in (errno.EAGAIN, errno.ENOMEM):
                return False # Can't fork anymore.
            raise
        if not pid:
            # Child
            child.close()
            # Put child into its own process group.
            pid = os.getpid()
            os.setpgid(pid, pid)
            # Restore signal handlers.
            self._restoreSignalHandlers()
            # Close copies of child sockets.
            for f in [x['file'] for x in self._children.values()
                      if x['file'] is not None]:
                f.close()
            self._children = {}
            try:
                # Enter main loop.
                self._child(sock, parent)
            except KeyboardInterrupt:
                pass
            sys.exit(0)
        else:
            # Parent
            parent.close()
            d = self._children[pid] = {}
            d['file'] = child
            d['avail'] = True
            return True