Multiprocessing question

Paul LaFollette paul.lafollette at gmail.com
Mon Jul 14 01:53:09 CEST 2014


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

-------------------------
Paul S. LaFollette, Jr
CIS Department
Temple University
+1 215 204 6822
paul.lafollette at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140713/e8270320/attachment.html>


More information about the Python-list mailing list