How to spawn parallel child processes on a multi-processor system?

前端 未结 4 1761
北海茫月
北海茫月 2020-12-04 06:52

I have a Python script that I want to use as a controller to another Python script. I have a server with 64 processors, so want to spawn up to 64 child processes of this sec

4条回答
  •  無奈伤痛
    2020-12-04 07:28

    I don't think you need queue unless you intend to get data out of the applications (Which if you do want data, I think it may be easier to add it to a database anyway)

    but try this on for size:

    put the contents of your create_graphs.py script all into a function called "create_graphs"

    import threading
    from create_graphs import create_graphs
    
    num_processes = 64
    my_list = [ 'XYZ', 'ABC', 'NYU' ]
    
    threads = []
    
    # run until all the threads are done, and there is no data left
    while threads or my_list:
    
        # if we aren't using all the processors AND there is still data left to
        # compute, then spawn another thread
        if (len(threads) < num_processes) and my_list:
            t = threading.Thread(target=create_graphs, args=[ my_list.pop() ])
            t.setDaemon(True)
            t.start()
            threads.append(t)
    
        # in the case that we have the maximum number of threads check if any of them
        # are done. (also do this when we run out of data, until all the threads are done)
        else:
            for thread in threads:
                if not thread.isAlive():
                    threads.remove(thread)
    

    I know that this will result in 1 less threads than processors, which is probably good, it leaves a processor to manage the threads, disk i/o, and other things happening on the computer. If you decide you want to use the last core just add one to it

    edit: I think I may have misinterpreted the purpose of my_list. You do not need my_list to keep track of the threads at all (as they're all referenced by the items in the threads list). But this is a fine way of feeding the processes input - or even better: use a generator function ;)

    The purpose of my_list and threads

    my_list holds the data that you need to process in your function
    threads is just a list of the currently running threads

    the while loop does two things, start new threads to process the data, and check if any threads are done running.

    So as long as you have either (a) more data to process, or (b) threads that aren't finished running.... you want to program to continue running. Once both lists are empty they will evaluate to False and the while loop will exit

提交回复
热议问题