Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'preferably': 0.05; '#include': 0.09; 'exec': 0.09; 'fashion.': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'thrown': 0.09; 'subject:question': 0.10; 'python': 0.11; 'skip:= 70': 0.12; 'thread': 0.14; '204': 0.16; 'clause.': 0.16; 'errno': 0.16; 'modules,': 0.16; 'proceeds': 0.16; 'reasonably': 0.16; 'stuff.': 0.16; 'threads,': 0.16; 'wrote:': 0.18; 'do.': 0.18; '<': 0.19; 'dependent': 0.19; 'unlike': 0.19; 'written': 0.21; 'seems': 0.21; 'creating': 0.23; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'char': 0.24; 'received:emailsrvr.com': 0.24; 'paul': 0.24; 'people,': 0.24; "i've": 0.25; '>': 0.26; 'received:(smtp server)': 0.26; 'url:edu': 0.26; 'header:In-Reply-To:1': 0.27; 'appreciated.': 0.29; 'work.': 0.31; 'code': 0.31; 'gary': 0.31; 'void': 0.31; 'run': 0.32; 'quite': 0.32; 'minimal': 0.33; 'could': 0.34; "can't": 0.35; 'something': 0.35; 'beyond': 0.35; 'but': 0.35; 'there': 0.35; 'doing': 0.36; 'useful': 0.36; 'so,': 0.37; 'skip:- 20': 0.37; 'skip:& 10': 0.38; 'thank': 0.38; 'process,': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'little': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'university': 0.39; 'even': 0.60; 'easy': 0.60; 'course.': 0.60; 'simple': 0.61; 'kind': 0.63; 'more': 0.64; 'sample': 0.67; 'between': 0.67; 'other.': 0.75; 'behavior': 0.77; 'gain': 0.79; '215': 0.84; 'forks': 0.84; 'subject:skip:M 10': 0.84; 'wakes': 0.84; 'waking': 0.84; 'temple': 0.91 X-Virus-Scanned: OK Date: Sun, 13 Jul 2014 17:07:21 -0700 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Multiprocessing question References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------000905000706060204030101" 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: 283 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1405297006 news.xs4all.nl 2832 [2001:888:2000:d::a6]:47648 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74408 This is a multi-part message in MIME format. --------------000905000706060204030101 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 07/13/2014 04:53 PM, Paul LaFollette wrote: > Kind people, > I have thrown together a little C/UNIX program that forks a child > process, then proceeds to let the child and parent alternate. Either > can run until it pauses itself and wakes the other. > > I would like to know if there be a way to create the same behavior in > Python 3, preferably in a non-platform dependent fashion. I would > prefer to use processes rather than threads, but could live with > threads if I had to. I've studied the documentation for the > multiprocessing and thread modules, but I can't see an easy way to do > what I want to do. I need minimal communication between processes > beyond what i have described, so creating queues or pipes seems like > overkill. Unlike what I have written here, I will want to exec the > child rather than write the whole thing in the else clause. Is there > a reasonably simple way to do this? A reference to useful > documentation would be appreciated. Sample code even more so, of course. > Thank you > Paul There is a quite reasonable way to do what you want, but that involves the multiprocessing and queue modules, both of which you have eliminated from consideration. So you have to ask yourself: What do you gain from using Python if you eliminate all the tools Python provides? Gary Herron > > ------------------------- > Paul S. LaFollette, Jr > CIS Department > Temple University > +1 215 204 6822 > paul.lafollette@temple.edu > http://knight.cis.temple.edu/~lafollet > > > #include > #include > #include > #include > #include > #include > > int main(int argc, char **argv) > { > void handler(int); > pid_t pid; > > signal(SIGCONT, handler); > > pid = fork(); > if (pid < 0) //failure > { > printf("Unable to fork\n"); > exit(1); > } > else if (pid > 0) // parent > { > while (1) > { > printf("Parent waiting for child to do something\n"); > pause(); > printf("Parent doing nifty stuff.\n"); > sleep(1); > printf("Parent waking child\n"); > errno = 0; > if (kill(pid, SIGCONT) < 0) > perror("Parent failed to SIGCONT child."); > } > } > else //pid == 0 so child > { > while (1) > { > printf (" Child doing useful > work.\n"); > sleep(1); > printf(" Child waking > parent\n"); > if (kill(getppid(), SIGCONT) < 0) > perror("Child failed to SIGCONT parent."); > printf(" Child waiting for > parent to do something\n"); > pause(); > } > } > } > > void handler(int signum) > { > } > =============================================================================== > Output: > Parent waiting for child to do something > Child doing useful work. > Child waking parent > Child waiting for parent to do > something > Parent doing nifty stuff. > Parent waking child > Parent waiting for child to do something > Child doing useful work. > Child waking parent > Child waiting for parent to do > something > Parent doing nifty stuff. > Parent waking child > Parent waiting for child to do something > Child doing useful work. > Child waking parent > Child waiting for parent to do > something > > > --------------000905000706060204030101 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
On 07/13/2014 04:53 PM, Paul LaFollette wrote:
Kind people,
I have thrown together a little C/UNIX program that forks a child process, then proceeds to let the child and parent alternate.  Either can run until it pauses itself and wakes the other.

I would like to know if there be a way to create the same behavior in Python 3, preferably in a non-platform dependent fashion.  I would prefer to use processes rather than threads, but could live with threads if I had to.  I've studied the documentation for the multiprocessing and thread modules, but I can't see an easy way to do what I want to do.  I need minimal communication between processes beyond what i have described, so creating queues or pipes seems like overkill.  Unlike what I have written here, I will want to exec the child rather than write the whole thing in the else clause.  Is there a reasonably simple way to do this?  A reference to useful documentation would be appreciated.  Sample code even more so, of course.
Thank you
Paul

There is a quite reasonable way to do what you want,  but that involves the multiprocessing and queue modules, both of which you have eliminated from consideration.

So you have to ask yourself:  What do you gain from using Python if you eliminate all the tools Python provides?

Gary Herron





-------------------------
Paul S. LaFollette, Jr
CIS Department
Temple University
+1 215 204 6822
paul.lafollette@temple.edu
http://knight.cis.temple.edu/~lafollet

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>

int main(int argc, char **argv)
{
  void handler(int);
  pid_t pid;

  signal(SIGCONT, handler);

  pid = fork();
  if (pid < 0) //failure
  {
    printf("Unable to fork\n");
    exit(1);
  }
  else if (pid > 0) // parent
  {
    while (1)
    {
      printf("Parent waiting for child to do something\n");
      pause();
      printf("Parent doing nifty stuff.\n");
      sleep(1);
      printf("Parent waking child\n");
      errno = 0;
      if (kill(pid, SIGCONT) < 0)
        perror("Parent failed to SIGCONT child.");
    }
  }
  else //pid == 0 so child
  {
    while (1)
    {
      printf ("                                    Child doing useful work.\n");
      sleep(1);
      printf("                                     Child waking parent\n");
      if (kill(getppid(), SIGCONT) < 0)
        perror("Child failed to SIGCONT parent.");
      printf("                                     Child waiting for parent to do something\n");
      pause();
    }
  }
}

void handler(int signum)
{
}
===============================================================================
Output:
Parent waiting for child to do something
                                    Child doing useful work.
                                     Child waking parent
                                     Child waiting for parent to do something
Parent doing nifty stuff.
Parent waking child
Parent waiting for child to do something
                                    Child doing useful work.
                                     Child waking parent
                                     Child waiting for parent to do something
Parent doing nifty stuff.
Parent waking child
Parent waiting for child to do something
                                    Child doing useful work.
                                     Child waking parent
                                     Child waiting for parent to do something




--------------000905000706060204030101--