Path: csiph.com!usenet.pasdenom.info!aioe.org!rt.uk.eu.org!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'received:209.85.223': 0.03; '"""': 0.05; 'cleanup': 0.07; 'decorator': 0.07; 'exit': 0.07; 'remaining': 0.07; 'try:': 0.07; 'api': 0.09; '%s\\n"': 0.09; '(%d)': 0.09; '**kwargs)': 0.09; '**kwargs):': 0.09; 'failed:': 0.09; 'fork': 0.09; 'oserror': 0.09; 'spawn': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'anyway': 0.11; 'slightly': 0.15; 'daemon,': 0.16; 'it..': 0.16; 'logging,': 0.16; 'module?': 0.16; 'proc': 0.16; 'processes.': 0.16; 'received:209.85.223.173': 0.16; 'reproduce': 0.16; 'subject:avoiding': 0.16; 'sys.exit(0)': 0.16; 'sys.exit(1)': 0.16; 'true:': 0.16; 'import': 0.21; 'terminate': 0.22; 'runs': 0.22; 'skip:_ 20': 0.22; 'cc:2**0': 0.23; 'second': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:m 30': 0.26; 'environment.': 0.27; 'implemented': 0.27; 'easiest': 0.27; 'message-id:@mail.gmail.com': 0.27; 'run': 0.28; 'parent': 0.29; 'url:mailman': 0.29; 'skip:_ 10': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'function': 0.30; 'sense': 0.31; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; '-----': 0.32; 'url:listinfo': 0.32; 'problem': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'thanks': 0.34; 'process,': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'except': 0.36; 'but': 0.36; 'wanted': 0.36; 'url:org': 0.36; 'method': 0.36; 'thank': 0.36; 'possible': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'perform': 0.38; 'store': 0.38; 'skip:l 20': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'gives': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'end': 0.40; 'url:mail': 0.40; 'you.': 0.61; 'first': 0.61; 'back': 0.62; 'leaving': 0.62; 'perfect': 0.63; 'ever': 0.63; 'information': 0.63; 'more': 0.63; 'url:blogspot': 0.64; 'here': 0.65; 'our': 0.65; 'person,': 0.65; 'below.': 0.68; 'disclose': 0.69; 'notice:': 0.71; 'privileged.': 0.72; 'killing': 0.84; 'listener': 0.84; 'medium.': 0.91; 'time..': 0.91; 'url:fr': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=MQC3PGcscyoXqakDDE9xH7iwKqamWxc2bouFHZTR+lE=; b=JKqUaPa4404Xy1Ve8tlZvF+vnK558w+QrHEVKRFHzRFo+hQ1N79wkaktcqF1eWIJv3 itVG4fKFUGj5SKZH1qc9mdrHUp6ImZEAdjP0PL7e3mOzIoaLnX1+vDa5yAdEWunJO85i du3GKvc8/OuI1pRTHizl9A3Ke/qpLgb9vDGg/3FAPROOEfESvA7ZxsT2iaT1siKE8qkO 52HJXbV3a/d2uxEgHHaJwFk+QRY5uhljpzVBgODyoy8ZkjGbAyBLuJ5SdFN4op2FqCHR twIkXu3m7C93o773FKkT8LpBSdqYXVIj1hn8KV4jY/DegkSIXvKNsWIZs0FGxkEXa8pl xx0g== MIME-Version: 1.0 In-Reply-To: <1859051376.1041601.1355236378205.JavaMail.root@sequans.com> References: <1859051376.1041601.1355236378205.JavaMail.root@sequans.com> Date: Tue, 11 Dec 2012 14:39:17 +0000 Subject: Re: forking and avoiding zombies! From: andrea crotti To: Jean-Michel Pichavant Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 116 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355236765 news.xs4all.nl 6852 [2001:888:2000:d::a6]:40490 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34621 2012/12/11 Jean-Michel Pichavant : > ----- Original Message ----- >> So I implemented a simple decorator to run a function in a forked >> process, as below. >> >> It works well but the problem is that the childs end up as zombies on >> one machine, while strangely >> I can't reproduce the same on mine.. >> >> I know that this is not the perfect method to spawn a daemon, but I >> also wanted to keep the code >> as simple as possible since other people will maintain it.. >> >> What is the easiest solution to avoid the creation of zombies and >> maintain this functionality? >> thanks >> >> >> def on_forked_process(func): >> from os import fork >> """Decorator that forks the process, runs the function and gives >> back control to the main process >> """ >> def _on_forked_process(*args, **kwargs): >> pid =3D fork() >> if pid =3D=3D 0: >> func(*args, **kwargs) >> _exit(0) >> else: >> return pid >> >> return _on_forked_process >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > Ever though about using the 'multiprocessing' module? It's a slightly hig= her API and I don't have issues with zombie processes. > You can combine this with a multiprocess log listener so that all logs ar= e sent to the main process. > > See Vinay Sajip's code about multiprocessing and logging, http://plumberj= ack.blogspot.fr/2010/09/using-logging-with-multiprocessing.html > > I still had to write some cleanup code before leaving the main process, b= ut once terminate is called on all remaining subprocesses, I'm not left wit= h zombie processes. > Here's the cleaning: > > for proc in multiprocessing.active_children(): > proc.terminate() > > JM > > > -- IMPORTANT NOTICE: > > The contents of this email and any attachments are confidential and may a= lso be privileged. If you are not the intended recipient, please notify the= sender immediately and do not disclose the contents to any other person, u= se it for any purpose, or store or copy the information in any medium. Than= k you. Yes I thought about that but I want to be able to kill the parent without killing the childs, because they can run for a long time.. Anyway I got something working now with this def daemonize(func): def _daemonize(*args, **kwargs): # Perform first fork. try: pid =3D os.fork() if pid > 0: sys.exit(0) # Exit first parent. except OSError as e: sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # Decouple from parent environment. # check if decoupling here makes sense in our case # os.chdir("/") # os.umask(0) # os.setsid() # Perform second fork. try: pid =3D os.fork() if pid > 0: return pid except OSError, e: sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # The process is now daemonized, redirect standard file descriptors= . sys.stdout.flush() sys.stderr.flush() func(*args, **kwargs) return _daemonize @daemonize def long_smarter_process(): while True: sleep(2) print("Hello how are you?") And it works exactly as before, but more correctly..