如何在amphp中正确运行多个并发进程?

时间:2018-10-04 07:35:14

标签: php amphp

我正在学习如何在amphp中进行并发。刚找到一个用例在我的测试中使用它。但是,从输出看来,我的代码似乎是同步运行的。

这是我的代码:

<?php
public function testPutDocument()
    {
        // sync all mapping
        $service = app(SearchService::class);
        $result = $service->syncMapping();
        $this->assertInternalType('array', $result);
        Loop::run(function () use ($service) {
            foreach (array_keys($this->domainProvider()) as $domain) {
                var_dump("dispatching $domain test..");
                Loop::defer(function () use ($domain, $service) {
                    var_dump("running $domain test..");
                    // build model
                    $class = __NAMESPACE__."\ModelBuilder\\".ucfirst($domain);
                    $message = "Please create '$class' class for testing '$domain' domain mapping.";
                    $this->assertTrue(class_exists($class), $message);
                    $this->buildModels(new $class)
                        ->each(function ($model) use ($domain, $service) {
                            // put document
                            try {
                                $result = $service->storeDocument($domain, $model);
                                $this->assertInstanceOf(Result::class, $result);
                            } catch (CredentialException $e) {
                                $message = $e->getMessage();
                                $message = "Failed put document for '$domain' domain. Details:\n\n$message";
                                $this->assertTrue(false, $message);
                            }
                            // delete document
                            try {
                                $result = $service->deleteDocument($domain, $model->id);
                                $this->assertInstanceOf(Result::class, $result);
                            } catch (CredentialException $e) {
                                $message = $e->getMessage();
                                $message = "Failed delete document for '$domain' domain. Details:\n\n$message";
                                $this->assertTrue(false, $message);
                            }
                        });
                    var_dump("done $domain test..");
                });
                var_dump("dispatched $domain test.");
            }
        });
    }

输出:

string(26) "dispatching contact test.."
string(24) "dispatched contact test."
string(30) "dispatching interaction test.."
string(28) "dispatched interaction test."
string(27) "dispatching timeline test.."
string(25) "dispatched timeline test."
string(26) "dispatching insight test.."
string(24) "dispatched insight test."
string(22) "running contact test.."
string(19) "done contact test.."
string(26) "running interaction test.."
string(23) "done interaction test.."
string(23) "running timeline test.."
string(20) "done timeline test.."
string(22) "running insight test.."
string(19) "done insight test.."

我以为如果代码是同时运行的,输出将是这样的:

string(26) "dispatching contact test.."
string(24) "dispatched contact test."
string(30) "dispatching interaction test.."
string(28) "dispatched interaction test."
string(27) "dispatching timeline test.."
string(25) "dispatched timeline test."
string(26) "dispatching insight test.."
string(24) "dispatched insight test."
string(22) "running contact test.."
string(26) "running interaction test.."
string(23) "running timeline test.."
string(22) "running insight test.."
string(19) "done contact test.."
string(23) "done interaction test.."
string(20) "done timeline test.."
string(19) "done insight test.."

另外,阻塞线是这一行:

$result = $service->storeDocument($domain, $model); 

一个POST请求

$result = $service->deleteDocument($domain, $model->id); 

那是一个删除请求

我想念什么?

0 个答案:

没有答案