Der Eindruck, dass skalierbarkeitsbewusste Programmierung und Sprachen wie Parallel / Parallel-Verarbeitung, Funktionstyp und Reaktiv in letzter Zeit Aufmerksamkeit erregen. Daher werde ich die grundlegende Schreibmethode der Mehrprozessverarbeitung unter Verwendung von drei Arten von Skriptsprachen zusammenfassen, die häufig in der Webentwicklung verwendet werden.
Vagrantfile
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "1024", "--cpus", "4", "--ioapic", "on"]
end
top -d1
#Drücken Sie 1, um alle CPUs anzuzeigen
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
#Prozessgenerierung
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
//Übergeordneter Prozess+Untergeordneter Prozess=4 Prozesse
$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) {
//Hier ist es in einen übergeordneten und einen untergeordneten Prozess unterteilt.
//Wenn es dem übergeordneten Prozess gelingt, einen untergeordneten Prozess zu erzeugen, wird die PID dieses Prozesses angezeigt_ICH WÜRDE
//Wenn du verlierst-Holen Sie sich 1
//Der untergeordnete Prozess erhält 0
$pid = pcntl_fork();
//Fehler beim Erstellen des untergeordneten Prozesses
if ($pid === -1) {
echo 'Failed process fork';
exit;
}
//Verarbeitung von untergeordneten Prozessen
if ($pid === 0) {
$res = [];
foreach(range(0, 100000) as $i) {
$res[] = md5(uniqid(mt_rand(), true));
}
echo 'created : '.getmypid()."\n";
exit;
}
//Verarbeitung des übergeordneten Prozesses
$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
Recommended Posts