Forking / Multi-Threaded Processes | Bash

前端 未结 8 1517
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 20:39

I would like to make a section of my code more efficient. I\'m thinking of making it fork off into multiple processes and have them execute 50/100 times at once, instead of

8条回答
  •  有刺的猬
    2020-11-30 20:52

    Here's my thread control function:

    #!/bin/bash
    # This function just checks jobs in background, don't do more things.
    # if jobs number is lower than MAX, then return to get more jobs;
    # if jobs number is greater or equal to MAX, then wait, until someone finished.
    
    # Usage:
    #   thread_max 8
    #   thread_max 0    # wait, until all jobs completed
    
    thread_max() {
        local CHECK_INTERVAL="3s"
        local CUR_THREADS=
        local MAX=
        [[ $1 ]] && MAX=$1 || return 127
    
        # reset MAX value, 0 is easy to remember
        [ $MAX -eq 0 ] && {
            MAX=1
            DEBUG "waiting for all tasks finish"
        }
    
        while true; do
            CUR_THREADS=`jobs -p | wc -w`
    
            # workaround about jobs bug. If don't execute it explicitily,
            # CUR_THREADS will stick at 1, even no jobs running anymore.
            jobs &>/dev/null
    
            DEBUG "current thread amount: $CUR_THREADS"
            if [ $CUR_THREADS -ge $MAX ]; then
                sleep $CHECK_INTERVAL
            else
                return 0
            fi
        done
    }
    

提交回复
热议问题