amphp / amp

A non-blocking concurrency framework for PHP applications. 🐘

Home Page:https://amphp.org/amp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

asyncCall Still blocking

eachtime opened this issue · comments

Hi! I'm trying to make submitting a form that sends emails non-blocking on a webpage.
I think amp is a good tool for that, am I right?
I tried putting the sending part in a call() and asyncCall() function as shown in the examples or in the doc. Everything works fine but the code is still blocking. Did I miss something? I read the doc a bit, but I cant figure it out.
Do we have to configure something for amp to work properly? a php unix socket or a php pool maybe?

Hey @eachtime, asyncCall will work asynchronously within the process as long as the event loop runs, but the event loop still needs to run, so it won't magically run things "somewhere later".

Ah! Thank you very much, I was sure I was missing something. I read about the event loop, but thought it was for other purposes. I haven't read how or where to start the loop in the doc though. Maybe you can direct me to a useful page? So it realy do work without messing with php at all. Outstanding!

You can run the event loop by invoking Loop::run() or Amp\Promise\wait on a promise.

Ok thanks @kelunik, I will try this.
Sorry for all the dumb questions. It must be obvious to everyone. I just read the event loop part of the doc and the examples, but I'm still pretty confused.
Do we have to manage the loop life cycle ourselves, like starting it and killing it on each request?
Or do we have to start it during a deployment script or something and not deal with it after that?

A real life example in the doc would be really great for noobs like me ;)

If you're using a traditional PHP deployment with mod_php or fpm, you'll need to run the event loop in each request separately, yeah. You'll probably want to use Amp\Promise\wait in these situations.

If you're using an application server like amphp/http-server, there'll be only a single call to Loop::run() in the entire application.

But it looks like you're more looking for a message queue and a worker rather than directly using an event loop in your application?

I'm using fpm.

But it looks like you're more looking for a message queue and a worker rather than directly using an event loop in your application?

Yes that's exactly it. Do you mean that amp is not the right tool for that?

You'll probably want to use Amp\Promise\wait in these situations.

"wait" seems a little bit counter-intuitive to me.

I think I'll just give it a try and see how it goes. So I just have to start the loop before sending an email and then close it when it is sent (inside the asyncCall function)?

I couldn't figure out how Amp\Promise\wait works so I tried with Loop::run() and Loop::stop() and the code is still blocking. Maybe you're right, I should try a queue and a worker, but it seems a little bit of an overkill just for a contact form. I will give it some thought. Anyway thank you very much @kelunik and I will probably try amp for other use cases because it look awesome!