amphp / ext-fiber

PHP Fiber extension
Other
239 stars 14 forks source link

I want to ask some questions #5

Closed thinker-gao closed 3 years ago

thinker-gao commented 3 years ago

First of all, thank you for your contribution to php. I would like to ask if this extension will be merged into the main branch of php via rfc? Does this extension support windows?

kelunik commented 3 years ago

@thinker-gao Yes, we plan to do that. You can find the draft of the RFC at https://github.com/trowski/fiber-rfc/blob/master/rfc.md

trowski commented 3 years ago

Does this extension support windows?

Yes, the extension supports Windows.

thinker-gao commented 3 years ago

Thank you for your contribution to php. In addition, I found some similar projects, such as swoole and swow. I hope to bring you reference and improvement. Thank you again for your contribution. swoole:https://github.com/swoole/swoole-src swow:https://github.com/swow/swow

kelunik commented 3 years ago

@thinker-gao We're aware of Swoole, but I wasn't aware of swow, thanks. Swoole seems to follow a different approach, especially considering channels are the only communication between coroutines.

As the questions have been answered, I'll close this issue.

codercms commented 3 years ago

@trowski @kelunik hi! I have a few questions too. I am advanced user of Swoole. And I'm interested in native PHP coroutines.

  1. Is this possible to achieve some of Swoole functionality? 1.1. Channel based messaging between coroutines and between timer -> coroutine? 1.2. Coroutine context - user level access to coroutine (fiber) context with an ability to read/write custom data?
  2. Ability to wait one of multiple communication operations (IO, channels, timers and etc) - https://tour.golang.org/concurrency/5
  3. Possibility to share data between threads? Currently the main limitation of Swoole is that it uses multi process architecture instead of multi threading architecture, it makes software development simpler, but it also imposes restrictions to maximum performance and memory efficiency. And currently Swoole offers only limited memory sharing features (Lock and Table). Do you have any plans to make scheduler in a multi threading way, like Golang does? Ofc I saw your mention of parallels extensions, but this extension doesn't provide full multi-threading, it just creates multiple PHP interpreter threads.
  4. And also I have to ask about debugging, is it possible to list all coroutines and see their stack trace?
  5. How do you plan do detect dead locks?
kelunik commented 3 years ago

Channel based messaging between coroutines and between timer -> coroutine?

Sure, there's an implementation in https://github.com/amphp/parallel. What do you mean with timer -> coroutine?

Coroutine context - user level access to coroutine (fiber) context with an ability to read/write custom data?

Yes, you can build this using Fiber::this() and weak maps.

Ability to wait one of multiple communication operations (IO, channels, timers and etc) - tour.golang.org/concurrency/5

This can easily be implemented using promises in userland, see e.g. Amp\Promise\first().

Possibility to share data between threads?

Yes, PHP keeps being single threaded and any data can be accessed normally. Fibers are 1:n scheduled on a kernel thread, not n:m on different kernel threads.

Do you have any plans to make scheduler in a multi threading way, like Golang does?

No, see above.

And also I have to ask about debugging, is it possible to list all coroutines and see their stack trace?

I think this isn't possible right now, but it's possible to build this into a debugger like xdebug or provide it as core functionality in the future.

How do you plan do detect dead locks?

There aren't any locks, as only one fiber is running at any given time. Thus, there's no need for any dead lock detection.

codercms commented 3 years ago

@kelunik thanks for your answers!

What do you mean with timer -> coroutine?

I mean communication from timer tick handler with some coroutine that is working in background. The answer for this question is https://github.com/amphp/parallel/blob/master/lib/Sync/Channel.php

Yes, you can build this using Fiber::this() and weak maps.

Did I understand correctly that I can put some data to fiber context and it will be automatically freed on fiber exit?

-- P.S. I would like to express my gratitude to you guys. You're really making PHP better and doing it in a right way, thanks!

kelunik commented 3 years ago

Yes, communication isn't limited and works normally. The version you've linked isn't based on fibers, take a look at the v2 branch for that.

You can't store data in the fiber context, but you can build something similar with weak maps, yes.