Comments (6)
出现这个错误的原因可能有很多,包括代码中的并发处理、Cygwin环境的兼容性问题或其他潜在的代码错误。这里有一些建议和调试步骤,可以帮助你解决这个问题:
-
确认Cygwin环境
确保你使用的是最新版本的Cygwin,并且所有相关的库和工具都已更新到最新版本。由于Swoole是在Linux上开发和优化的,可能会出现与Windows/Cygwin兼容性的问题。 -
检查并发处理
并发处理是造成问题的常见原因,尤其是在处理大量数据时。你可以尝试减少并发量,以确认是否是由于并发引起的错误。 -
增加错误处理和日志记录
在代码中添加更多的错误处理和日志记录,以便更好地了解在哪一步出现了错误。例如,在数据库操作和Curl操作中增加更多的错误检查和日志记录。 -
逐步测试
逐步测试每个部分,以便确定问题的根源。先测试Curl操作,然后测试数据库插入操作,最后再将两者结合起来。
调试示例
以下是改进后的代码示例,增加了一些错误处理和日志记录:
php
Copiază codul
from swoole-src.
你试试高版本的swoole-cli会不会出现这个问题
from swoole-src.
你试试高版本的swoole-cli会不会出现这个问题
swoole-cli-v5.0.3 一样有这个问题
from swoole-src.
我发现在上面代码的基础上加\Swoole\Coroutine::sleep(0.1); 就没有报错
全部代码如下:
<?php
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;
use Swoole\Coroutine\Channel;
use Swoole\Coroutine\WaitGroup;
class MyCurl
{
public static $fail_arr = [];
public function curl_test($set)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1?set=' . $set);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
// var_dump(curl_getinfo($ch));
// var_dump(curl_error($ch));
curl_close($ch);
return $result;
}
}
class MyUseMysql
{
public static $instert_arr = [];
private static $conn;
public function __construct()
{
if (empty(self::$conn)) {
$swoole_mysql = new Swoole\Coroutine\MySQL();
$swoole_mysql->connect([
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'root',
'password' => 'root',
'database' => 'test',
]);
self::$conn = $swoole_mysql;
}
}
public function setMsg($msg)
{
$sql = "INSERT INTO `cache` (`msg`) VALUES ('$msg');";
self::$conn->begin();
self::$conn->query($sql);
self::$conn->commit();
}
}
run(function () {
// 创建管道
$channel = new Channel(1);
// 协程数量
$go_count = 100;
// 模拟数据
$pc_arr = [];
for ($pc = 1; $pc <= 250; $pc++) {
$pc_arr[] = $pc;
}
// 分成100份模拟数据(100个协程)
$chunks = array_chunk($pc_arr, ceil(count($pc_arr) / $go_count));
//等待协程
$wg = new WaitGroup();
// 100个协程生产数据
foreach ($chunks as $chunk_key => $chunk_value) {
$wg->add();
go(function () use ($chunk_key, $chunk_value, $channel, $wg) {
// 执行1份数据(2500/100=25个)
$myCurlObj = new MyCurl();
foreach ($chunk_value as $pc_value) {
$result_curl = $myCurlObj->curl_test($chunk_key . "_" . $pc_value);
if ($result_curl === false) {
MyCurl::$fail_arr[] = $pc_value;
} else {
//
var_dump("fromWeb:" . $result_curl);
$channel->push($result_curl);
}
}
$wg->done();
});
}
// 一个协程消费数据
go(function () use ($channel) {
$myUseMysql=new MyUseMysql();
while (true) {
// 判断管道是否存在
$data = $channel->pop();
// var_dump($channel);
// 判断管道是否关闭
if ($channel->errCode === SWOOLE_CHANNEL_CLOSED) break;
if ($channel->errCode === SWOOLE_CHANNEL_TIMEOUT) continue;
if ($data) {
\Swoole\Coroutine::sleep(0.1);
var_dump("toMysql:" . $data);
$myUseMysql->setMsg($data);
}
}
});
// 等待所有协程任务完成
$wg->wait();
//关闭管道
$channel->close();
// 输出失败数组
var_dump("失败数组:");
var_dump(MyCurl::$fail_arr);
});
究竟是为什么?
from swoole-src.
Related Issues (20)
- Websocket connection failed. SSL related Issue? HOT 1
- Swoole not supporting partitioned cookie parameter HOT 1
- Can allow_redirects be supported on the HTTP/WebSocket? HOT 7
- Alpine + Swoole + PgSQL on the host = `could not send SSL negotiation packet: Resource temporarily unavailable` HOT 16
- 已解决
- 开启一键协程化之后, `proc_open()` 似乎会受到先前空方法体的 `run(function() {});` 的影响。 HOT 1
- Worker exits without much information HOT 3
- Websocket服務,調用Task做訊息發送,出現session does not exits HOT 5
- \Swoole\Server::shutdown never gets called and signals don't seem to work with \Swoole\Process::signal() / pcntl_signal() HOT 4
- A bug occurred in Swoole-v5.1.2, please report it HOT 26
- PHP Startup Unable to load dynamic library 'swoole' HOT 2
- guzzlehttp在stream=true时,设置超时无效 HOT 5
- 协程被长时间挂起会导致内存大量增加 HOT 49
- windows swoole-cli-v4.8.13-cygwin-x64.zip 在windows上 run(function{}) 内的curl 问题 HOT 2
- 在 Swoole\Coroutine\Http\Server 中的 http server 使用 sendfile 在高并发时会导致 PHP 假死并且内存泄露 HOT 57
- 🪶 在 C++ 中,有提供了Socket sendfile的方法(zerocopy),可以不通过CPU去调度资源,避免重复拷贝两次DMZ(或者更多) HOT 15
- syntax error near unexpected token `SQLITE,' HOT 4
- @swoole/library/ext/sockets.php(217) E_DEPRECATED:Creation of dynamic property Swoole\Coroutine\Socket::$__ext_sockets_nonblock is deprecated HOT 3
- MACOS M1 SIGRTMIN HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swoole-src.