amphp / amp

A non-blocking concurrency framework for PHP applications. 🐘
https://amphp.org/amp
MIT License
4.25k stars 257 forks source link

Any way to create or get a context for coroutine. #419

Closed xpader closed 1 year ago

xpader commented 1 year ago

In Swoole extension, the Swoole\Coroutine class has getContext(), getCid(), getPcid(), that can help to create a context for the coroutine. What's that mean? that mean we can use like 'Request::get()' to get input variables like $_GET, or User::getCurrentUser() without pass $request object to the method. We can store some values in the context for coroutine and don't need pass the source object in all the call stack.

kelunik commented 1 year ago

You can use FiberLocals for that, see https://github.com/revoltphp/event-loop/blob/main/examples/fiber-local-automatic.php

xpader commented 1 year ago

That's great, I'll try that.

xpader commented 1 year ago

I have create a Context to do that: https://github.com/wind-framework/wind-framework/blob/fafe298933405a511fcc297ef4a1079020b6779d/src/base/Context.php. It works well for request instance in https://github.com/wind-framework/web/blob/24eb5d3a2d7b1299911db3caea8f8c4cd2fac59e/src/Request.php#L314

I have another question, FiberLocal implement base on Fiber::getCurrent(), does Fiber has a parent Fiber or sub Fiber? Is this may broke FiberLocal.

kelunik commented 1 year ago

There's no such concept like a parent context currently.

xpader commented 1 year ago

Thanks.