Multi-threading com loop [duplicado]

0

Isso faz parte de um código de script bash que eu desejo usar multi-threading

for DATABASE in $DATABASES ; do
    echo Converting $DATABASE
    # Check if the table is MyISAM (we don't want to convert InnoDB tables over and over again)
    TABLES=$(echo "SELECT TABLE_NAME FROM information_schema.TABLES where TABLE_SCHEMA = '$DATABASE' and ENGINE = 'MyISAM'" | $MYSQL_COMMAND)
    for TABLE in $TABLES ; do
        echo Converting MyISAM $TABLE to InnoDB
        echo "ALTER TABLE $TABLE ENGINE = INNODB" | $MYSQL_COMMAND $DATABASE
    done
    if [ "x$TABLES" = "x" ] ; then
        echo No MyISAM tables found in $DATABASE database
    fi
    echo
done

É possível que FOR use multi-threading?

Eu estava pensando em reescrever o script e fazê-lo com xargs -P3 -n1 -I{} e assim por diante, mas parece sujo para mim.

    
por Luka 18.04.2018 / 16:43

1 resposta

0

Eu fiz isso!

function convert_db() {
    echo Converting $1
    # Check if the table is MyISAM (we don't want to convert InnoDB tables over and over again)
    TABLES=$(echo "SELECT TABLE_NAME FROM information_schema.TABLES where TABLE_SCHEMA = '$1' and ENGINE = 'MyISAM'" | $MYSQL_COMMAND)
    for TABLE in $TABLES ; do
        echo Converting MyISAM $TABLE to InnoDB
        echo "ALTER TABLE $TABLE ENGINE = INNODB" | $MYSQL_COMMAND $1
    done
    if [ "x$TABLES" = "x" ] ; then
        echo No MyISAM tables found in $1 database
    fi
    echo
}

for DATABASE in $DATABASES ; do
convert_db $DATABASE &
done
    
por 18.04.2018 / 17:01