Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74406

Multiprocessing question

Date 2014-07-13 19:53 -0400
Subject Multiprocessing question
From Paul LaFollette <paul.lafollette@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.11791.1405295598.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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@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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Multiprocessing question Paul LaFollette <paul.lafollette@gmail.com> - 2014-07-13 19:53 -0400
  Re: Multiprocessing question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-14 02:44 +0000
    Re: Multiprocessing question Roy Smith <roy@panix.com> - 2014-07-14 08:59 -0400

csiph-web