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


Groups > comp.lang.python > #88252

Re: Run two processes in parallel

References <mf8c4p$71p$1@ger.gmane.org>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2015-03-29 03:13 -0600
Subject Re: Run two processes in parallel
Newsgroups comp.lang.python
Message-ID <mailman.302.1427620460.10327.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Mar 29, 2015 at 2:11 AM, Thorsten Kampe
<thorsten@thorstenkampe.de> wrote:
> Hi,
>
> I'd like to run two processes concurrently (either through a builtin
> module or a third-party). One is a "background" task and the other is
> displaying a spinner (for which I already found good packages).
>
> The two processes do not have to communicate with each other; only
> the second should be able to know when the first has terminated. To
> make it clear, this is the pseudo-code:
>
> ```
> import time, PARALLEL, spinner
> process1 = PARALLEL.start(time.sleep(60))
>
> while process1.isrunning
>     spinner.update()
>     time.sleep(1)
>
> print('done')
> ```
>
> From what I see, Python modules threading and multiprocessing seem
> either not suited for that or to be too complicated (though I don't
> have any experience with these and could be wrong). A search on PyPi
> for parallel, background, and concurrent found too many.
>
> What would you do? Which technique or module would you suggest?

It shouldn't be all that complicated:

import time, multiprocessing, spinner
process1 = multiprocessing.Process(target=time.sleep, args=(60,))
process1.start()

while process1.is_alive():
    spinner.update()
    time.sleep(1)

# join is called implicitly by is_alive, but
# calling it explicitly is good practice.
process1.join()
print('done')

Threading should also work fine as long as your background thread
doesn't hold the GIL for long periods of time, which would freeze out
your spinner update loop. I would tend to prefer threading over
multiprocessing for this since the spinner thread is not CPU-bound.

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


Thread

Re: Run two processes in parallel Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-29 03:13 -0600

csiph-web