与PHP和mySQL的异步事务

当网站上发生某个特定的动作(用PHP和MySQL构建)时,我希望发生一些异步事情。 例如,当用户回答问题,上传文档时,我希望在后台发生很少的异步事情,例如:
1.update用户的分数
2。 在Facebook wall
3.track用户操作上发布消息。</ p>

哪种PHP,JavaScript和MySQL技术最适合实现这一目标? 请帮忙。</ p>

我想要一些MySQL触发但想要异步发生的东西。</ p>
</ div>

展开原文

原文

When a certain action happens on the site (built in PHP and MySQL) I would like few asynchronous things to happen on the site.

For example, when a user answers a question, uploads document, I want few asynchronous things to happen in the background such as: 1.update user's score 2. Post a message on Facebook wall 3.track users actions.

What PHP, JavaScript and MySQL technologies are best for achieving this? Please help.

I want something that MySQL triggers but wanted to happen asynchronously.

5个回答

Use the swoole extension. https://github.com/matyhtf/swoole

asyc mysql example:

$db = new mysqli;
$db->connect('127.0.0.1', 'root', 'root', 'test');
$db->query("show tables", MYSQLI_ASYNC);
swoole_event_add(swoole_get_mysqli_sock($db), function($__db_sock) {
    global $db;
    $res = $db->reap_async_query();
    var_dump($res->fetch_all(MYSQLI_ASSOC));
    exit;
});



我能想到的唯一两个选项是消息队列和异步ajax。 </ p>

第一个选项通过将作业放入数据库来工作,然后计划的php程序将循环遍历数据库中的每个作业并运行它们。</ p>

第二个是使用ajax调用php程序。</ p>

对于第一个选项,请查看以下内容: http://alanstorm.com/simple_php_job_queue

第二次看看jquery及其ajax选项: http://api.jquery.com/jQuery.ajax/ </ p>
</ div>

展开原文

原文

The only two options I can think of are a messaging queue, and async ajax.

The first option works by putting jobs into a database, then a scheduled php program will loop through every job in the database and run them.

The second is just calling a php program using ajax.

For the first option look at something like: http://alanstorm.com/simple_php_job_queue
For the second look at jquery, and its ajax options: http://api.jquery.com/jQuery.ajax/

The best option is to call three separate PHP scripts using exec and passing the output to null, like below.

exec("/usr/bin/php somescript.php >> some_log_file.log 2>&1 &");

Doing the same exec on three scripts for your three functions will execute, all three things concurrently/asynchronously. This is what your after.

If you add jobs to a job queue and then have a cron clear that queue, your not really doing it asynchronously. Your still doing things one by one, in a synchronous blocking fashion - although the user is not waiting for these things to happen. There might be a delay in the queue being cleared as well if your site has high traffic, you won't get the same delay with exec. Since the script will be executed immediately and concurrently to the initial request.

Side Note: This is why Node.js is becoming so popular it's built on JavaScript which is designed for this kind of development approach. Asynchronous and event driven.



您可以使用消息队列服务器。</ p>
</ div>

展开原文

原文

You could use a message queue server.



你也可以使用php系统调用在后台运行另一个程序: http://php.net/manual/en/function.system.php </ p>
</ div>

展开原文

原文

also you can use php system call to run another program in background: http://php.net/manual/en/function.system.php

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐