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


Groups > comp.lang.python > #74408

Re: Multiprocessing question

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 <gary.herron@islandtraining.com>
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; '&lt;': 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; '&gt;': 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 <gary.herron@islandtraining.com>
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 <CA+dsdf0iwt_Z9iDoFTUu8XD6m70onQ3Gb2BoDDU_fjY9qF=gmA@mail.gmail.com>
In-Reply-To <CA+dsdf0iwt_Z9iDoFTUu8XD6m70onQ3Gb2BoDDU_fjY9qF=gmA@mail.gmail.com>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11792.1405297006.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

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


Thread

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

csiph-web