File: done.rst

package info (click to toggle)
python-sh 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 900 kB
  • sloc: python: 4,157; makefile: 25
file content (24 lines) | stat: -rw-r--r-- 573 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Here's an example of using :ref:`done` to create a multiprocess pool, where
``sh.your_parallel_command`` is executed concurrently at no more than 10 at a
time:

.. code-block:: python

    import sh
    from threading import Semaphore

    pool = Semaphore(10)

    def done(cmd, success, exit_code):
        pool.release()

    def do_thing(arg):
        pool.acquire()
        return sh.your_parallel_command(arg, _bg=True, _done=done)

    procs = []
    for arg in range(100):
        procs.append(do_thing(arg))

    # essentially a join
    [p.wait() for p in procs]