forked from farwish/swoole-wholly
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/** | ||
* task.php | ||
* | ||
* github.com/farwish/swoole-wholly | ||
* | ||
* @author ercom | ||
*/ | ||
|
||
$server = new Swoole\Server("0.0.0.0", 7749, SWOOLE_PROCESS, SWOOLE_SOCK_TCP); | ||
|
||
$server->set([ | ||
'worker_num' => 2, | ||
'task_worker_num' => 2, | ||
]); | ||
|
||
$server->on('WorkerStart', function ($server, $workerId) { | ||
if ($workerId == 0) { | ||
$data = [1, 2, 3, 4, 5]; | ||
foreach ($data as $value) { | ||
echo "Send task data {$value}\n"; | ||
$server->task($value); | ||
} | ||
} | ||
}); | ||
|
||
$server->on("Receive", function ($server, $fd, $reactorId, $data) { | ||
}); | ||
|
||
$server->on('Task', function ($server, $taskId, $srcWorkerId, $data) { | ||
sleep(1); | ||
echo "Task#{$taskId} execute task, data is {$data}\n"; | ||
return "aaa{$data}"; | ||
}); | ||
|
||
$server->on('Finish', function ($server, $taskId, $data) { | ||
echo "Task{$taskId} execute finish, data is {$data}\n"; | ||
}); | ||
|
||
$server->start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Swoole 协程 | ||
|
||
## CSP 编程方式 | ||
|
||
CSP 编程 | ||
|
||
``` | ||
不同于传统的通过共享内存来通信,CSP 讲究以通信的方式来共享内存。 | ||
Swoole 使用到了 CSP 里面的部分概念,参考了 Golang 的实现,使用 go 协程作为执行体, | ||
用 Chan 作为实体间通信的通道,Defer 在协程退出时执行。 | ||
``` | ||
|
||
Swoole 协程特点 | ||
|
||
``` | ||
应用层使用同步的编程方式,底层自动实现异步 IO 的效果和性能。 | ||
不需要在应用层使用 yield 关键字标识协程切换,易于使用。 | ||
默认开启了 enable_coroutine 选项,底层会在一些回调函数中自动创建一个协程,此时回调中使用协程 API。 | ||
使用 Coroutine::create 或者 go 方法来手动创建一个协程。 | ||
协程的切换是隐式发生的,所以协程切换前后不保证全局变量和静态变量的一致性(不安全)。 | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
/** | ||
* co.php | ||
* | ||
* github.com/farwish/swoole-wholly | ||
* | ||
* @author ercom | ||
*/ | ||
|
||
Swoole\Coroutine::set([ | ||
'max_coroutine' => 2000, | ||
]); | ||
|
||
for ($i = 0; $i < 1000; $i++) { | ||
go(function () { | ||
echo 'A'; | ||
co::sleep(5); | ||
echo 'B'; | ||
}); | ||
} |