Skip to content

Speed up python multicore code #8719

Closed
Closed

Description

@kripken

We use Python's multiprocessing.Pool (see get_multiprocessing_pool in tools/shared.py) to compile system libraries etc., so that we use all cores. This may be slower on windows. Perhaps there is a faster way to execute a bunch of shell commands in parallel?

Activity

  1. quantum5 commented on Jun 12, 2019

    @quantum5
    Member

    If we are just running commands, a platform non-specific way would be to use threads. This should be as easy as using multiprocessing.ThreadPool instead of multiprocessing.Pool.

    On POSIX, using select/poll/epoll/kqueue would probably be more efficient than a thread or process pool, but that is highly non-trivial.

    Thing to think about with thread pools:

    • we must make sure we aren't doing any computation heavy things in python with a thread pool, since python code is run with the GIL, but I/O is fine
    • multiprocessing forks (on POSIX) or spawns a child process and sends things to it (on Windows), if we mutate global state, it would be shared across all workers. This may force us to make certain things thread local or require a mutex, but it should not be a concern if we are just executing subprocesses.
  2. kripken commented on Jun 12, 2019

    @kripken
    MemberAuthor

    Hmm, there doesn't seem to be multiprocessing.ThreadPool. Is it in a library perhaps?

    It does sound like that could speed us up, as we do very little python in those pools, it's mostly calling external things like clang, node, etc. - which we mostly do by creating a new process for each - which is also something maybe we should optimize.

  3. quantum5 commented on Jun 13, 2019

    @quantum5
    Member

    Sorry, I meant multiprocessing.pool.ThreadPool. It's not exactly documented, but it's the pool used by the dummy version of multiprocessing. It has been around for a long time and I don't think it will be removed anytime soon.

  4. quantum5 commented on Jun 14, 2019

    @quantum5
    Member

    I found no visible improvement on Linux with a thread pool. I do not have a Windows machine to check if the same holds true there.

    #8794 contains the code used to test the thread pool.

    On a side note, significant improvement in build performance is found with #8795.

  5. quantum5 commented on Jun 18, 2019

    @quantum5
    Member

    There appears to be no visible improvement on Windows either.

  6. sbc100 commented on Jun 18, 2019

    @sbc100
    Collaborator

    It think it should be easier to develop and maintain code using a thread pool vs a multi-process pool. We've run into many issue in the past with the way results get serialized back from multiprocess pools.

    If each of the child jobs is simply a subprocess there is no point in adding the extra python process so I would support moving to threads. Not sure about using an undocumented API though. Does python not have a thread pool builtin that isn't part of multiprocessing?

  7. stale commented on Jun 17, 2020

    @stale

    This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions