Impression que la programmation et les langages sensibles à l'évolutivité tels que le traitement parallèle / parallèle, le type fonctionnel et le réactif attirent récemment l'attention. Par conséquent, je vais résumer la méthode d'écriture de base du traitement multi-processus en utilisant trois types de langages de script souvent utilisés dans le développement Web.
Vagrantfile
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "1024", "--cpus", "4", "--ioapic", "on"]
end
top -d1
#Appuyez sur 1 pour voir tous les processeurs
Ruby
require "digest/md5"
require "securerandom"
require 'benchmark'
pcount = 4
def single(pcount)
(pcount - 1).times do
arr = []
100000.times do
arr << Digest::MD5.digest(SecureRandom.uuid)
end
end
end
def multi(pcount)
pids = []
(pcount - 1).times do
#Génération de processus
pids << fork do
arr = []
100000.times do
arr << Digest::MD5.digest(SecureRandom.uuid)
end
end
end
Process.waitall
end
single_time = Benchmark.realtime do
single(pcount)
end
print single_time.to_s() + "\n"
multi_time = Benchmark.realtime do
multi(pcount)
end
print multi_time.to_s() + "\n"
Cpu0 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu0 : 99.0%us, 1.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 99.0%us, 1.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 : 99.0%us, 1.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Python
import multiprocessing as mp
import hashlib
import uuid
import time
import os
def create_hash(res=[]):
print('created: ' + str(os.getpid()))
for _ in range(100000):
res.append(hashlib.md5(str(uuid.uuid4()).encode('utf8')).hexdigest())
return res
def single(pcount):
start_time = time.time()
print('parent: ' + str(os.getpid()))
res = []
for _ in range(pcount):
res = create_hash(res)
end_time = time.time()
print('all finished : ' + str(end_time - start_time))
def multi(pcount):
start_time = time.time()
print('parent: ' + str(os.getpid()))
processes = []
for _ in range(pcount):
processes.append(mp.Process(target=create_hash, args=()))
for process in processes:
process.start()
for process in processes:
process.join()
end_time = time.time()
print('all finished : ' + str(end_time - start_time))
if __name__ == '__main__':
pcount = 4
single(pcount)
multi(pcount)
Cpu0 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu0 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3 : 99.0%us, 1.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
PHP
//Processus parent+Processus enfant=4 processus
$pcount = 3;
function single($pcount) {
$time_start = microtime(true);
$res = [];
echo 'parent : '.getmypid()."\n";
foreach(range(0, $pcount - 1) as $i) {
echo 'created : '.getmypid()."\n";
foreach(range(0, 100000) as $ii) {
$res[] = md5(uniqid(mt_rand(), true));
}
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "all finished : $time\n";
}
function multi($pcount) {
$processes = [];
$time_start = microtime(true);
echo 'parent : '.getmypid()."\n";
foreach(range(0, $pcount - 1) as $p) {
//Ici, il est divisé en un processus parent et un processus enfant.
//Si le processus parent réussit à générer un processus enfant, le PID de ce processus_ID
//Si vous échouez-Obtenez 1
//Le processus enfant obtient 0
$pid = pcntl_fork();
//Échec de la création du processus enfant
if ($pid === -1) {
echo 'Failed process fork';
exit;
}
//Traitement des processus enfants
if ($pid === 0) {
$res = [];
foreach(range(0, 100000) as $i) {
$res[] = md5(uniqid(mt_rand(), true));
}
echo 'created : '.getmypid()."\n";
exit;
}
//Traitement du processus parent
$processes[] = $pid;
}
foreach($processes as $process) {
pcntl_waitpid($process, $status);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "all finished : $time\n";
}
single($pcount);
multi($pcount);
Cpu0 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 : 0.0%us, 1.0%sy, 0.0%ni, 99.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3 : 0.0%us, 1.0%sy, 0.0%ni, 99.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu0 : 1.0%us, 0.0%sy, 0.0%ni, 99.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 99.0%us, 0.0%sy, 0.0%ni, 1.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 : 97.0%us, 1.0%sy, 0.0%ni, 2.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3 : 96.1%us, 1.0%sy, 0.0%ni, 2.0%id, 0.0%wa, 0.0%hi, 1.0%si, 0.0%st
--Il existe également une méthode multi-processus pour les langages de script.
Recommended Posts