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


Groups > comp.lang.python > #74408 > unrolled thread

Re: Multiprocessing question

Started byGary Herron <gary.herron@islandtraining.com>
First post2014-07-13 17:07 -0700
Last post2014-07-14 08:07 +0300
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Multiprocessing question Gary Herron <gary.herron@islandtraining.com> - 2014-07-13 17:07 -0700
    Re: Multiprocessing question Marko Rauhamaa <marko@pacujo.net> - 2014-07-14 08:07 +0300

#74408 — Re: Multiprocessing question

FromGary Herron <gary.herron@islandtraining.com>
Date2014-07-13 17:07 -0700
SubjectRe: Multiprocessing question
Message-ID<mailman.11792.1405297006.18130.python-list@python.org>

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

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 <mailto:paul.lafollette@temple.edu>
> http://knight.cis.temple.edu/~lafollet 
> <http://knight.cis.temple.edu/%7Elafollet>
>
> #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
>
>
>

[toc] | [next] | [standalone]


#74415

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-07-14 08:07 +0300
Message-ID<87sim45xzu.fsf@elektro.pacujo.net>
In reply to#74408
Gary Herron <gary.herron@islandtraining.com>:

> On 07/13/2014 04:53 PM, Paul LaFollette wrote:
>> 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.
>>
>> [...]
>
> What do you gain from using Python if you eliminate all the tools
> Python provides?

The language core itself.

Anyway, the subprocessing.Popen is perfect for process creation. For
event multiplexing, Python3.4 has the asyncio module, although
multiplexing pipes might be platform-dependent (works in linux at
least).


Marko

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web