PHP在单独的线程中发送POST请求而忘记了 [英] PHP send POST request in separate thread and forget

查看:75
本文介绍了PHP在单独的线程中发送POST请求而忘记了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在处理预订,并且我希望在处理过程中将预订详细信息发送给我的应用程序的任何合作伙伴进行预订,因此他们可以存储对预订的引用, AND 我的应用必须完成的其他处理.

My app processes bookings, and upon process I would like it to send the booking details to whichever of my app's partners made the booking, so they can store a reference to the booking, AND without hold up the other processing my app has to do.

我已经考虑过如何向合作伙伴发送消息,而我的解决方案是向进行预订的任何合作伙伴发送cURL POST请求(除了回答我的问题,也许有人会更好解决方案?).

I've thought about how to send a message to a partner, and my solution would be to send a cURL POST request to whichever of my partner's is making the booking (besides answering my question maybe someone has a better solution than this?).

每个合作伙伴都有一个特定的URL,可以将其设置为接收此POST请求并存储我们发送给他们的预订信息.

Each partner would have a specific URL that they would setup to receive this POST request and store the booking information we send them.

问题: 如果我们尝试发送此POST请求,而他们的Web服务器速度缓慢或宕机,则我们可能不必要地等待很长时间才能获得响应,这反过来又会延迟确认使用我们服务的实际用户的预订.

THE PROBLEM: If we try send this POST request and their web server is slow or down then we may wait unnecessarily long to get a response, which in turn would delay the confirmation of the booking of the actual user making use of our service.

理想解决方案: 我想在另一个线程中发送此PHP cURL请求,因此我们可以继续我们愉快的方式并确认预订.如果在另一个线程中有延迟,则不会阻止我们.

IDEAL SOLUTION: I would like to send this PHP cURL request in another thread so we can continue on our merry way and confirm the booking. If in the other thread there is a delay, this will not hold us up.

我考虑过的其他解决方案是:

Other solutions I have considered are:

  • 调用外部脚本(例如用python编写)以发送此请求.我已经读到,使用 exec()可能会占用大量资源.我们有很多预订,因此我们将发送很多这样的预订POST.因此,理想情况下,我们需要某种资源意识.
  • 使用套接字.我对这些配置不熟悉,并且担心我们的套接字服务器出现故障.这也感觉像是一项维护任务.也许我错了?
  • 使用类似 Pusher 之类的服务,该服务实际上是套接字服务.缺点是,如果侦听器错过了一条消息,他们将永远不会再收到它.例如.合作伙伴会错过存储该预订的机会.
  • Calling an external script (e.g. written in python) to send this request. I've read that using exec() can be very resource intensive. We have lots of bookings so we would be sending lots of these booking POSTs. So ideally we need something resource conscience.
  • Using sockets. I'm not familiar with the configuration of these and I'm worried about our socket server being down. It also feels like a mission to maintain. Maybe I'm wrong?
  • Using a service like Pusher which is effectively a socket service. The disadvantage is that if the listener misses a message they will never get it again. E.g. the partner would miss storing that booking.

非常高兴能收到我在这里要完成的工作的反馈,特别是从需要解决我所遇到的类似情况的人那里获得反馈.谢谢您的帮助!

It would be great to get some feedback about what I'm trying to accomplish here, especially from someone that has been in need of a solution for the same kind of situation I'm in. Thanks for any help!

推荐答案

因此,实质上,您是在PHP中创建其他客户端使用的API.这是我的建议:

So essentially you are creating an API in PHP that other clients consume. here is what I would suggest:

  1. 让您的客户通过POST/GET方法向您发出请求;而不是您尝试将数据推送到客户端的API服务器.那是更好的方法,因为它可以使您摆脱客户端服务器故障,速度慢或其他问题.因此,当他们向您发送请求时,这意味着他们完全有能力处理响应.

  1. Let your clients make requests to you, via POST/GET method; instead of you as a API server trying to push data to your clients. that is much better approach because it frees you with things client's server down, slow or something else. So when they send you a request, it means they are fully capable for handling response.

使用HTTP持久连接:在Apache中,其名为keep-alive的值设置为高,因此客户端可以重用现有的连接&因此减少了等待时间.

use HTTP persistent connection: in apache its called keep-alive set its value to high, so clients can reuse existing connection & thus reduced latency.

要在php中进行多处理,请查看 进入多处理.基本上,有pcntl_fork()函数可让您分叉进程&创建新的子进程以进行多处理.

For multiprocessing in php, have a look at Getting into multiprocessing. Basically, there is pcntl_fork() function which allows you to fork a process & create new child process for multiprocessing.

基于redis或类似的东西实施后台作业que.想法是所有长期运行的作业都将落入后台作业que&中.然后为每个任务生成一个工作程序,以便通过多处理执行这些作业. 具有Redis&独奏

Implement a background job que based on redis or something similar. idea is that all long running jobs gets dropped into the background job que & then a worker is spawned for each task so these jobs are executed via multiprocessing. PHP Workers with Redis & Solo

希望有帮助

这篇关于PHP在单独的线程中发送POST请求而忘记了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆