คอนเทนเนอร์หลายกระบวนการ Snidel ช่วยให้นักพัฒนา PHP ทุกคนทำงานกับการประมวลผลแบบขนานได้ง่ายขึ้น โดยไม่มีส่วนขยายใด ๆ

โปรดพิจารณาบริจาคให้กับ Akihito Nakano ผู้แต่งโครงการนี้เพื่อแสดง❤และการสนับสนุนของคุณ
สปอนเซอร์ @Ackintosh บนสปอนเซอร์ GitHub
(en)
ไม่กี่คนเริ่มต้นการเขียนโปรแกรมของพวกเขาด้วย PHP และดำเนินต่อไป การประมวลผลแบบขนานพวกเขาไม่คุ้นเคยกับมันและอาจเป็นอุปสรรค์สำหรับพวกเขา
มิฉะนั้นคนที่ จำกัด การพัฒนาด้วยภาษาที่ไม่ใช่ PHP (เช่นภาษาที่มีคุณสมบัติที่เหนือกว่าสำหรับการประมวลผลแบบขนาน) (เป็นฉันในอดีต)
เพื่อให้การประมวลผลแบบขนานง่ายขึ้นและสัญชาตญาณให้พวกเขาใช้ฉันเริ่มพัฒนา Snidel
Snidel สามารถเป็นหนึ่งในตัวเลือกของคุณเมื่อคุณกำลังพิจารณาว่า "จะทำอย่างไรให้ขนานกัน" เป็นเกียรติสำหรับฉัน
(JA)
ฉันคิดว่ามีโปรแกรมเมอร์บางคนที่เริ่มเขียนโปรแกรมด้วย PHP และได้สร้างอาชีพของพวกเขาด้วย PHP (ฉัน) สำหรับคนเช่นนี้การประมวลผลแบบขนานอาจไม่คุ้นเคยกับมันหรืออาจดูเหมือนว่ามันเป็นเกณฑ์ที่สูง
อีกวิธีหนึ่งมีคนที่ต้องดำเนินการพัฒนาในสถานการณ์ที่พวกเขาถูก จำกัด ไม่ให้ใช้ภาษาอื่นนอกเหนือจาก PHP (ตัวอย่างเช่นภาษาที่มีกลไกที่ยอดเยี่ยมสำหรับการประมวลผลแบบขนาน) เนื่องจากสถานการณ์ต่าง ๆ (นี่เป็นกรณีของฉันมาก่อน)
เราได้เริ่มพัฒนา Snidel โดยมีจุดประสงค์เพื่อทำให้ง่ายขึ้นและเป็นไปได้อย่างมากในการแก้ปัญหาโดยใช้การประมวลผลแบบขนาน
ฉันหวังว่า Snidel สามารถเป็นหนึ่งในตัวเลือกของคุณได้ถ้าคุณพูดว่า "ฉันต้องการเรียกใช้กระบวนการนี้ในแบบคู่ขนานฉันควรทำอย่างไร?"
$ composer require ackintosh/snidel:~0.11.0

นอกจากนี้ยังเป็นไปได้ที่การประมวลผลแบบขนานผ่านฟังก์ชั่น Build-in (เช่น exec ):
initialize_data_required_for_the_slow_jobs ();
exec ( ' php slow_job1.php & ' );
exec ( ' php slow_job2.php & ' );สำหรับนักพัฒนาที่รู้สึกว่า "ความเจ็บปวด" กับข้างต้น Snidel สามารถมอบประสบการณ์ที่ดีงามและจะปรับปรุงการเขียนโปรแกรม PHP ของพวกเขา
เราจะเดินผ่านการใช้งานเพื่อแสดงให้เห็นว่าการประมวลผลแบบคู่ขนานละลายในการเขียนโปรแกรมของคุณอย่างไร ประสบการณ์ที่ใช้ Snidel ควรแก้ไขความเจ็บปวดของคุณ เริ่มต้นกันเถอะ!
<?php
use Ackintosh Snidel ;
$ f = function ( $ s ) {
sleep ( 3 );
echo ' echo: ' . $ s ;
return ' return: ' . $ s ;
};
$ s = time ();
$ snidel = new Snidel ();
$ snidel -> process ( $ f , [ ' foo ' ]);
$ snidel -> process ( $ f , [ ' bar ' ]);
$ snidel -> process ( $ f , [ ' baz ' ]);
// `Snidel::results()` returns `Generator`
foreach ( $ snidel -> results () as $ r ) {
// string(9) "echo: foo"
var_dump ( $ r -> getOutput ());
// string(11) "return: foo"
var_dump ( $ r -> getReturn ());
}
// If you don't need the results, let's use `Snidel::wait()` instead of `Snidel::results()`
// $snidel->wait();
echo ( time () - $ s ) . ' sec elapsed ' . PHP_EOL ;
// 3sec elapsed.พารามิเตอร์ทั้งหมดเป็นทางเลือก
new Snidel ([
' concurrency ' => 3 ,
// Please refer to `Logging`
' logger ' => $ monolog ,
// Please refer to `Using custom queue`
' driver ' => $ driver ,
// a polling duration(in seconds) of queueing
' pollingDuration ' => 1 ,
]);call_user_func_array // multiple arguments
$ snidel -> process ( $ f , [ ' arg1 ' , ' arg2 ' ]);
// global function
$ snidel -> process ( ' myfunction ' );
// instance method
$ snidel -> process ([ $ instance , ' method ' ]); $ f = function ( $ arg ) {
return $ arg ;
};
$ snidel -> process ( $ f , ' arg-A_tag1 ' , ' tag1 ' );
$ snidel -> process ( $ f , ' arg-B_tag1 ' , ' tag1 ' );
$ snidel -> process ( $ f , ' arg_tag2 ' , ' tag2 ' );
foreach ( $ snidel -> results as $ r ) {
// `Task::getTag()` returns the tag passed as 3rd parameter of `Snidel::process()`
switch ( $ r -> getTask ()-> getTag ()) {
case ' tag1 ' :
$ r -> getReturn (); // arg-A_tag1 | arg-B_tag1
break ;
case ' tag2 ' :
$ r -> getReturn (); // arg_tag2
break ;
default :
$ r -> getReturn ();
break ;
}
}Snidel รองรับการเข้าสู่ระบบด้วยเครื่องบันทึกซึ่งใช้ PSR-3: อินเตอร์เฟส Logger
// e.g. MonoLog
use Monolog Formatter LineFormatter ;
use Monolog Handler StreamHandler ;
use Monolog Logger ;
$ monolog = new Logger ( ' sample ' );
$ stream = new StreamHandler ( ' php://stdout ' , Logger:: DEBUG );
$ stream -> setFormatter ( new LineFormatter ( " %datetime% > %level_name% > %message% %context% n" ));
$ monolog -> pushHandler ( $ stream );
$ snidel = new Snidel ([ ' logger ' => $ monolog ]);
$ snidel -> process ( $ f );
// 2017-03-22 13:13:43 > DEBUG > forked worker. pid: 60018 {"role":"master","pid":60017}
// 2017-03-22 13:13:43 > DEBUG > forked worker. pid: 60019 {"role":"master","pid":60017}
// 2017-03-22 13:13:43 > DEBUG > has forked. pid: 60018 {"role":"worker","pid":60018}
// 2017-03-22 13:13:43 > DEBUG > has forked. pid: 60019 {"role":"worker","pid":60019}
// 2017-03-22 13:13:44 > DEBUG > ----> started the function. {"role":"worker","pid":60018}
// 2017-03-22 13:13:44 > DEBUG > ----> started the function. {"role":"worker","pid":60019}
// ... $ snidel -> process ( function ( $ arg1 , $ arg2 ) {
exit ( 1 );
}, [ ' foo ' , ' bar ' ]);
$ snidel -> get ();
var_dump ( $ snidel -> getError ());
// class AckintoshSnidelError#4244 (1) {
// ...
// }
foreach ( $ snidel -> getError () as $ pid => $ e ) {
var_dump ( $ pid , $ e );
}
// int(51813)
// array(5) {
// 'status' => int(256)
// 'message' => string(50) "an error has occurred in child process.
// 'callable' => string(9) "*Closure*"
// 'args' =>
// array(2) {
// [0] => string(3) "foo"
// [1] => string(3) "bar"
// }
// 'return' => NULL
// }
// } Snidel ขึ้นอยู่กับเบอร์นาร์ดเป็นเลเยอร์ที่เป็นนามธรรมคิว เบอร์นาร์ดเป็นไลบรารี PHP หลายแบ็คสำหรับการสร้างงานพื้นหลังสำหรับการประมวลผลในภายหลัง
โดยค่าเริ่มต้น Snidel จะสร้างไดรเวอร์ FlatFile แต่จากมุมมองสภาพการแข่งขันเราขอแนะนำให้ใช้คิวที่เชื่อถือได้มากขึ้นในการผลิต
$ connection = Aws Sqs SqsClient:: factory ([
' key ' => ' your-aws-access-key ' ,
' secret ' => ' your-aws-secret-key ' ,
' region ' => ' the-aws-region-you-choose '
]);
$ driver = new Bernard Driver SqsDriver ( $ connection );
new Snidel ([
' driver ' => $ driver ,
]);สำหรับรายละเอียดเกี่ยวกับคนขับโปรดดูที่นี่
นี่คือบทความที่แนะนำ Snidel ขอบคุณ!
| คนดมกลิ่น | PHP |
|---|---|
| 0.1 ~ 0.8 | > = 5.3 |
| 0.9 ~ | > = 5.6 |
| 0.13 | > = 7.1 |
เราขอแนะนำให้คุณลองใช้ Docker เนื่องจาก Snidel ต้องการส่วนขยาย PHP บางส่วนที่แสดงในข้อกำหนด
curl -Ss https://getcomposer.org/installer | php
docker build -t snidel .
docker run --rm -v ${PWD} :/snidel snidel php composer.phar install
docker run --rm -v ${PWD} :/snidel snidel vendor/bin/phpunit Snidel © Ackintosh เปิดตัวภายใต้ใบอนุญาต MIT
เขียนและดูแลโดย Ackintosh
GitHub @Ackintosh / Twitter @NAKANO_AKIHITO / บล็อก (JA)
รายการบล็อกโดยผู้แต่งเกี่ยวกับ Snidel (JA):
ขอบคุณ Jetbrains ที่ให้การสนับสนุนเราด้วยใบอนุญาตโอเพนซอร์สฟรี