有没有一种在PHP中实现多线程模型的现实方法,无论是真实的,还是仅仅模拟它.一段时间后,有人建议你可以强制操作系统加载另一个PHP可执行文件实例并处理其他同步进程.
这个问题是当PHP代码完成执行时,PHP实例仍然在内存中,因为没有办法从PHP中删除它.因此,如果您正在模拟几个线程,您可以想象会发生什么.所以我仍然在寻找一种可以在PHP中有效地完成或模拟多线程的方法.有任何想法吗?
我正在尝试进行AJAX调用(通过JQuery),这将启动一个相当长的过程.我希望脚本只是发送一个响应,表明进程已经启动,但是在PHP脚本运行完毕之前,JQuery不会返回响应.
我用"关闭"标题(下面)尝试了这个,还有输出缓冲; 似乎都不起作用.任何猜测?或者这是我在JQuery中需要做的事情?
<?php
echo( "We'll email you as soon as this is done." );
header( "Connection: Close" );
// do some stuff that will take a while
mail( 'dude@thatplace.com', "okay I'm done", 'Yup, all done.' );
?>
Run Code Online (Sandbox Code Playgroud) 如何让PHP 5.2(作为apache mod_php运行)向客户端发送完整的HTTP响应,然后继续执行操作一分钟?
长篇故事:
我有一个PHP脚本,必须执行一些长数据库请求并发送电子邮件,运行需要45到60秒.该脚本由我无法控制的应用程序调用.我需要应用程序报告从PHP脚本收到的任何错误消息(主要是无效的参数错误).
应用程序的超时延迟小于45秒(我不知道确切的值)因此将PHP脚本的每次执行都注册为错误.因此,我需要PHP尽可能快地将完整的HTTP响应发送到客户端(理想情况下,只要输入参数已经过验证),然后运行数据库和电子邮件处理.
我正在运行mod_php,所以pcntl_fork不可用.我可以通过将要处理的数据保存到数据库并运行实际过程来解决这个问题cron,但我正在寻找一个更短的解决方案.
我有一个PHP脚本,只生成日志到客户端.
当我回应某些内容时,我希望它能够即时转移到客户端.
(因为在脚本处理过程中,页面是空白的)
,我已经打得四处ob_start()和ob_flush(),但他们没有工作.
什么是最好的解决方案?
PS:在echo通话结束时放一个同花牌有点脏......
编辑:Answers既不工作,PHP或Apache错误?
我有一个WCF服务,第一次调用它需要很长时间来处理,然后缓存这些结果HttpRuntime.Cache.为了初始化这个缓存,我想从javascript触发一个fire-and-forget ajax调用.
现在我在页面中有这个javascript:
$.ajax({
type: 'GET',
url: getServiceURL() + 'PrimeCacheAjax',
contentType: 'application/json; charset=utf-8'
});
Run Code Online (Sandbox Code Playgroud)
当PrimeCacheAjax函数只是执行伪呼叫来填充缓存.
这种方法的唯一问题是具有此ajax调用的页面是一种登录页面,它执行一些javascript,打开另一个窗口并关闭自己.当窗口在服务器响应之前自行关闭时,我在fiddler中看到一个已取消的请求.我担心这可能会导致ajax呼叫无法到达服务器的情况,这可能吗?
有没有办法指定(使用$.ajax())没有响应,或者它真的不重要?
我正在尝试使用Slack Custom命令,并且不太确定如何使用延迟消息,因为Yoda Speak External API需要3秒以上的响应时间.
我做了以下事情:
/Yoda在我的情况下发送了slack命令并收到了reponse_url.post对响应URL 使用以下内容.Run Code Online (Sandbox Code Playgroud)$data_string = '{"response_type": "in_channel", "text":"Checking,please wait..."}' ; $chs = curl_init(); curl_setopt($chs, CURLOPT_URL, $response_url); curl_setopt($chs, CURLOPT_POST, true); curl_setopt($chs, CURLOPT_POSTFIELDS, $data_string); curl_setopt($chs, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($chs, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($chs, CURLOPT_RETURNTRANSFER, true); curl_setopt($chs, CURLOPT_POST, 1); curl_setopt($chs, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); $results = curl_exec($chs);
Run Code Online (Sandbox Code Playgroud)$chsres = curl_init(); curl_setopt($chsres, CURLOPT_URL, "https://yoda.p.mashape.com/yoda?sentence=welcome+to+stack"); curl_setopt($chsres, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($chsres, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($chsres, CURLOPT_VERBOSE, true); curl_setopt($chsres, CURLOPT_TIMEOUT, 45); curl_setopt($chsres, CURLOPT_RETURNTRANSFER, true); curl_setopt($chsres, CURLOPT_HTTPHEADER, array('Content-Type:application/json', "X-Mashape-Key:> …
我看了看这个问题.我正在寻找一种方法来做那个问题的OP也想要的,那就是在发送http响应之后继续处理php,但是在Symfony2中.
我实现了一个在每次内核终止后触发的事件.到目前为止一切都那么好,但我想要的是它在CERTAIN终止后,在特定的控制器动作中触发,例如在发送表单之后,而不是每次请求时.这是因为我想在某些时候做一些繁重的任务,并且不希望最终用户等待页面加载.
知道我该怎么办?
<?php
namespace MedAppBundle\Event;
use JMS\DiExtraBundle\Annotation\InjectParams;
use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Tag;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use JMS\DiExtraBundle\Annotation\Inject;
/**
* Class MedicListener
* @package MedAppBundle\EventListener
* @Service("medapp_test.listener")
* @Tag(name="kernel.event_subscriber")
*/
class TestListener implements EventSubscriberInterface
{
private $container;
private $logger;
/**
* Constructor.
*
* @param ContainerInterface $container A ContainerInterface instance
* @param LoggerInterface $logger A LoggerInterface instance
* @InjectParams({
* "container" = @Inject("service_container"),
* "logger" = @Inject("logger")
* })
*/ …Run Code Online (Sandbox Code Playgroud) 我开始编程电报机器人,我遇到了问题.当我发送/启动命令时,它向我发送一条欢迎消息(正如我编程的那样),但它不发送一次!它像循环一样无休止地发送它!这是来源:
<?php
define('API_KEY','<token>');
function makereq($method,$datas=[])
{
$url = "https://api.telegram.org/bot".API_KEY."/".$method;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($datas));
$res = curl_exec($ch);
if(curl_error($ch)){
var_dump(curl_error($ch));
}else{
return json_decode($res);
}
}
$website = "https://api.telegram.org/bot".API_KEY;
$update = json_decode(file_get_contents('php://input'));
$chat_id = $update->message->chat->id;
$message_id = $update->message->message_id;
$from_id = $update->message->from->id;
$name = $update->message->from->first_name;
$username = $update->message->from->username;
$textmessage = isset($update->message->text)?$update->message->text:'';
$reply = $update->message->reply_to_message->forward_from->id;
$stickerid = $update->message->reply_to_message->sticker->file_id;
$messageEntity = $update->messageentity->type;
function SendMessage($ChatId, $TextMsg)
{
makereq('sendMessage',[
'chat_id'=>$ChatId,
'text'=>$TextMsg,
'parse_mode'=>"MarkDown"]
);
}
if($textmessage == '/start')
{
SendMessage($chat_id,'<welcome message>');
}
?>
Run Code Online (Sandbox Code Playgroud) 我正在尝试从函数向客户端发送响应数据并继续执行。我按照下面的代码
ignore_user_abort(true);
set_time_limit(0);
ob_start();
// do initial processing here
echo $response; // send the response
header('Connection: close');
header('Content-Length: '.ob_get_length());
ob_end_flush();
ob_flush();
flush();
// check if fastcgi_finish_request is callable
if (is_callable('fastcgi_finish_request')) {
fastcgi_finish_request();
}
Run Code Online (Sandbox Code Playgroud)
从问题中得到代码
我得到的只是 200 ok 响应,而不是我回显的数据。
我也需要获取响应数据。我使用的是php7.1。php5和7的使用有什么区别吗?
请帮忙
我想在制作所有硬处理数据之前向最终用户返回响应。
我想简单地获取用户的请求,返回响应,它可以是一个简单的 json,
用户将看到数据,但在我的服务器端,我将继续其余的处理,调用分析,更改数据库等。
像这样的想法: 发送http响应后继续处理php
php ×9
ajax ×2
httpresponse ×2
jquery ×2
symfony ×2
amazon-ses ×1
apache ×1
asynchronous ×1
bots ×1
curl ×1
echo ×1
email ×1
fastcgi ×1
flush ×1
fork ×1
javascript ×1
slack ×1
slack-api ×1
telegram ×1
telegram-bot ×1
timeout ×1
wcf ×1