Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamedResponse not working #41

Closed
mfauveau opened this issue May 8, 2018 · 6 comments
Closed

StreamedResponse not working #41

mfauveau opened this issue May 8, 2018 · 6 comments

Comments

@mfauveau
Copy link

mfauveau commented May 8, 2018

Please answer these questions before submitting your issue. Thanks!

  1. Please provide your PHP and Swoole version. (php -v and php --ri swoole)
    PHP 7.2.0-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Nov 30 2017 13:58:33) ( NTS )
    Copyright (c) 1997-2017 The PHP Group
    Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.2.0-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies
    with blackfire v1.18.0~linux-x64-non_zts72, https://blackfire.io, by SensioLabs

swoole

swoole support => enabled
Version => 2.1.3
Author => tianfeng.han[email: mikan.tenny@gmail.com]
coroutine => enabled
epoll => enabled
eventfd => enabled
timerfd => enabled
signalfd => enabled
cpu affinity => enabled
spinlock => enabled
rwlock => enabled
async http/websocket client => enabled
Linux Native AIO => enabled
pcre => enabled
zlib => enabled
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled

Directive => Local Value => Master Value
swoole.aio_thread_num => 2 => 2
swoole.display_errors => On => On
swoole.use_namespace => On => On
swoole.use_shortname => On => On
swoole.fast_serialize => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608
2. Please provide your Laravel/Lumen version.
Laravel 5.6.21

  1. Which release version of this package are you using?
    2.3.4

  2. What did you do? If possible, provide a recipe for reproducing the error.
    SomeController@show

$response = new \Symfony\Component\HttpFoundation\StreamedResponse(function() {
    echo 'test';
}, 200, []);
return $response->send();
  1. What did you expect to see?
    The string "test" being echoed. (code works with nginx/php-fpm)

  2. What did you see instead?
    0 bytes are returned and server returns a 500.

@albertcht
Copy link
Member

Hi @mfauveau ,

Thanks for your bug report. I'm working on fixing it.

@albertcht albertcht added the bug label May 9, 2018
@albertcht
Copy link
Member

Hi @mfauveau ,

I fixed it in the commit e63e9d3, and will merge into the next release.

However, the streamed response is synchronous now, so it's not recommended to do some heavy output in the response.

I am considering to make it asynchronous in the future.

@albertcht albertcht added the fixed label May 9, 2018
@albertcht
Copy link
Member

This fix has been merged into the newest release.

@joecohens
Copy link

Hi @albertcht, thanks for the fixing this so quickly! What would it take to make it asynchronous?

@albertcht
Copy link
Member

albertcht commented May 10, 2018

Hi @joecohens ,

Basically you can push stream response to a Swoole task queue. The task delivering is totally asynchronous, but the process in task worker is still synchronous. At least it will not block the normal worker process.

Or you can create a process for stream response, and bind it to Swoole event, after the process is done, destroy the event binding. But in this way will consume more CPU resource.

So I'm still trying to find a balance.

@laraveladmin-cn
Copy link

Route::get('/streamswoole', function () {
    $class = '\\Symfony\\Component\\HttpFoundation\\StreamedResponse';
    $response = new $class(function (){
        $open_ai_key = 'sk-5OL';
        $open_ai = new OpenAi($open_ai_key);
        $txt = "";
        $opts = [
            'model' => 'gpt-3.5-turbo',
            'messages' => json_decode('[{
	"role": "system",
	"content": "You are a helpful assistant."
}, {
	"role": "user",
	"content": "今天星期几?"
}]',true),
            'temperature' => 1.0,
            'max_tokens' => 100,
            'frequency_penalty' => 0,
            'presence_penalty' => 0,
            'stream' => true
        ];
        $complete = $open_ai->chat($opts, function ($curl_info, $data) use (&$txt) {
            if ($obj = json_decode($data) and $obj->error->message != "") {
                error_log(json_encode($obj->error->message));
            } else {
                echo $data;
                $clean = str_replace("data: ", "", $data);
                $arr = json_decode($clean, true);
                if ($data != "data: [DONE]\n\n" and isset($arr["choices"][0]["delta"]["content"])) {
                    $txt .= $arr["choices"][0]["delta"]["content"];
                }
            }

            echo PHP_EOL;
            @ob_flush();
            @flush();
            return strlen($data);
        });
    },200,[
        'Access-Control-Allow-Origin'=>'*',
        'X-Accel-Buffering'=>'no',
        'Content-type'=>'text/event-stream',
        'Cache-Control'=>'no-cache'
    ]);
    return $response->send();
});
 sendMessage(){
            const eventSource = new EventSource('/streamswoole');
            const div = this.$refs['message'];

            eventSource.onmessage =  function (e){
                dd(e,1);
                if (e.data == "[DONE]") {
                    eventSource.close();
                } else {
                    let txt = JSON.parse(e.data).choices[0].delta.content
                    if (txt !== undefined) {
                        div.innerHTML += txt.replace(/(?:\r\n|\r|\n)/g, '<br>');
                    }
                }
            };
            eventSource.onerror =  function(e) {
                dd(e,2);
                eventSource.close();
            };
            dd(eventSource,3);
        }

浏览器显示错误结果:

1694019591688
1694019688169
1694019744958

laravel不使用swoole的情况是对的:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants