php stream set blocking,开启协程后,stream_set_blocking 被强制设定阻塞状态了?

### 问题描述

在 swoole协程应用中, 使用 [`symfony/process`](https://packagist.org/packages/symfony/process) 库遇到问题,`stream_set_blocking($resource, 0)` 设置非阻塞无效,导致应用库不能按预期执行.

### Swoole版本,PHP版本,以及操作系统版本信息

```

~$ php --ri swoole

swoole

Swoole => enabled

Author => Swoole Team

Version => 4.5.2

Built => Jul 17 2020 14:01:02

coroutine => enabled

epoll => enabled

eventfd => enabled

signalfd => enabled

cpu_affinity => enabled

spinlock => enabled

rwlock => enabled

openssl => OpenSSL 1.1.1g 21 Apr 2020

pcre => enabled

mutex_timedlock => enabled

pthread_barrier => enabled

futex => enabled

async_redis => enabled

Directive => Local Value => Master Value

swoole.enable_coroutine => On => On

swoole.enable_library => On => On

swoole.enable_preemptive_scheduler => Off => Off

swoole.display_errors => On => On

swoole.use_shortname => On => On

swoole.unixsock_buffer_size => 8388608 => 8388608

~$ uname -a && php -v && gcc -v

Linux xiemaomao 4.19.104-microsoft-standard #1 SMP Wed Feb 19 06:37:35 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

PHP 7.4.9 (cli) (built: Aug 7 2020 14:29:36) ( NTS )

Copyright (c) The PHP Group

Zend Engine v3.4.0, Copyright (c) Zend Technologies

with Zend OPcache v7.4.9, Copyright (c), by Zend Technologies

with Sdebug v2.9.3-dev, Copyright (c) 2002-2020, by Derick Rethans

Using built-in specs.

COLLECT_GCC=gcc

COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper

OFFLOAD_TARGET_NAMES=nvptx-none

OFFLOAD_TARGET_DEFAULT=1

Target: x86_64-linux-gnu

Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.5.0-3ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu

Thread model: posix

gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)

```

### 相关代码

```php

\Swoole\Runtime::enableCoroutine(true);

go(function () {

@proc_open('echo hello && sleep 3000 && echo world', [ ['pipe', 'r',],['pipe', 'w',], ['pipe', 'w',], ],$pipes);

foreach ($pipes as $pipe) {

stream_set_blocking($pipe, 0); //开启协程后,非阻塞设定都无效了.

}

var_dump(fread($pipes[1], 1024)); //当前会阻塞读取

var_dump(fread($pipes[1], 1024));

});

```

### 你期待的结果是什么?实际看到的错误信息又是什么?

不要影响 `stream_set_blocking` 非阻塞设置.

```php

\Swoole\Runtime::enableCoroutine(true);

go(function () {

@proc_open('echo hello && sleep 3000 && echo world', [ ['pipe', 'r',],['pipe', 'w',], ['pipe', 'w',], ],$pipes);

foreach ($pipes as $pipe) {

stream_set_blocking($pipe, 0); // 实现非阻塞

}

var_dump(fread($pipes[1], 1024)); // 实现非阻塞读取

var_dump(fread($pipes[1], 1024));

});

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值