php多進程插入數據
個人在虛擬機器centos7,單核,1G記憶體
/** * 模拟并发请求,10万次写入数据库 * 拆分为10个进程,每个进程处理一万条插入 */ $total = 10000; $num = 10; $per = $total/$num; $sql = ''; $child = ''; echo 'start '.microtime(true).PHP_EOL; for($i = 1; $i<= $num; $i++) { $pid = pcntl_fork(); if($pid == -1) { die('fork error'); } if($pid > 0) { //$id = pcntl_wait($status,WNOHANG); $child[] = $pid; } else if ($pid == 0) { $link = mysqli_connect('localhost','root','root','yii2advanced'); $start = ($i-1)*$per + 1; $end = $start + $per; for($j = $start; $j< $end; $j++){ $time = microtime(true); $sql = 'insert pcntl_test (rank,time) values ('.$j.','.$time.')'; mysqli_query($link,$sql); } mysqli_close($link); $id = getmypid(); echo 'child '.$id.' finished '.microtime(true).PHP_EOL; exit(0); } } while(count($child)){ foreach($child as $k => $pid) { $res = pcntl_waitpid($pid, $status, WNOHANG); if ( -1 == $res || $res > 0) { unset($child[$k]); } } } echo 'end '.microtime(true).PHP_EOL;
當$total=10000,$num = 10;執行結果如下:
start 1491903371.5548 child 19860 finished 1491903417.2113 child 19857 finished 1491903417.6909 child 19864 finished 1491903417.7793 child 19855 finished 1491903417.8695 child 19859 finished 1491903417.9162 child 19861 finished 1491903418.0089 child 19856 finished 1491903418.0532 child 19863 finished 1491903418.0842 child 19862 finished 1491903418.1474 child 19858 finished 1491903418.4341 end 1491903418.4424 总时间为46.88759994506836秒
當$total=10000,$ num = 100時,執行結果如下:
start 1491904334.1735 child 20085 finished 1491904337.0712 child 20086 finished 1491904337.144 …… child 20262 finished 1491904341.5602 child 20264 finished 1491904341.5803 end 1491904341.5869 总时间为7.413399934768677
當$total=10000,$num = 1000時,執行結果如下:
start 1491904562.0166 child 20282 finished 1491904562.1191 child 20277 finished 1491904562.1268 child 20279 finished 1491904562.1352 ... child 21586 finished 1491904576.6954 child 21582 finished 1491904576.7024 child 21584 finished 1491904576.7226 end 1491904576.7297 总时间为14.71310019493103,相比100个子进程,耗时更长了。进程切换太多,影响了了效率应该是原因之一。
當$total=100000 ,$num=100時,十萬筆記錄,100個進程插入
start 1491905670.2652 child 21647 finished 1491905725.4382 child 21651 finished 1491905725.4595 child 21642 finished 1491905725.5402 .... child 21810 finished 1491905729.7709 child 21812 finished 1491905729.8498 child 21811 finished 1491905729.9612 end 1491905729.9679 总时间为59.70270013809204
單一進程插入1萬個數據,耗時18秒,相對10個進程插入1萬個記錄來說,耗時少一點。
而單進程插入10萬筆記錄,耗時187.40066790581,相對來說,是挺慢的了。三分鐘。 。 。
不過,本人再fork 1000個進程,來插入10萬記錄時,成功的情況下36秒左右,也可能會出現錯誤,mysqli_connection返回false,是不是連線數受限了?
fork 一萬個子進程,插入一百萬個數據,這時,出現連接錯的情況就很多了。最後耗時360秒,在資料表中插入了945,300筆記錄,成功率94.53%。於是查看資料庫的相關設定資訊
mysql> show global status like '%connect%'; +-----------------------------------------------+---------------------+ | Variable_name | Value | +-----------------------------------------------+---------------------+ | Aborted_connects | 0 | | Connection_errors_accept | 0 | | Connection_errors_internal | 0 | | Connection_errors_max_connections | 628 | | Connection_errors_peer_address | 0 | | Connection_errors_select | 0 | | Connection_errors_tcpwrap | 0 | | Connections | 16519 | | Locked_connects | 0 | | Max_used_connections | 501 | | Max_used_connections_time | 2017-04-12 15:19:54 | | Performance_schema_session_connect_attrs_lost | 0 | | Ssl_client_connects | 0 | | Ssl_connect_renegotiates | 0 | | Ssl_finished_connects | 0 | | Threads_connected | 4 | +-----------------------------------------------+---------------------+ mysql> show global variables like '%connect%'; +-----------------------------------------------+--------------------+ | Variable_name | Value | +-----------------------------------------------+--------------------+ | character_set_connection | utf8mb4 | | collation_connection | utf8mb4_general_ci | | connect_timeout | 10 | | disconnect_on_expired_password | ON | | init_connect | | | max_connect_errors | 100 | | max_connections | 500 | | max_user_connections | 0 | | performance_schema_session_connect_attrs_size | 512 | +-----------------------------------------------+--------------------+ 修改 myqsql 配置文件,/etc/my.cnf 把max_connections 改为10000,然后重启mysql 实际MySQL服务器允许的最大连接数16384; 结果然并卵,虚拟机好像挂了了。
並發量大的時候,問題就出在連接mysql這裡了。
可以透過一個連線池來嘗試解決該問題。
個人在虛擬機器centos7,單核,1G記憶體
/** * 模拟并发请求,10万次写入数据库 * 拆分为10个进程,每个进程处理一万条插入 */ $total = 10000; $num = 10; $per = $total/$num; $sql = ''; $child = ''; echo 'start '.microtime(true).PHP_EOL; for($i = 1; $i<= $num; $i++) { $pid = pcntl_fork(); if($pid == -1) { die('fork error'); } if($pid > 0) { //$id = pcntl_wait($status,WNOHANG); $child[] = $pid; } else if ($pid == 0) { $link = mysqli_connect('localhost','root','root','yii2advanced'); $start = ($i-1)*$per + 1; $end = $start + $per; for($j = $start; $j< $end; $j++){ $time = microtime(true); $sql = 'insert pcntl_test (rank,time) values ('.$j.','.$time.')'; mysqli_query($link,$sql); } mysqli_close($link); $id = getmypid(); echo 'child '.$id.' finished '.microtime(true).PHP_EOL; exit(0); } } while(count($child)){ foreach($child as $k => $pid) { $res = pcntl_waitpid($pid, $status, WNOHANG); if ( -1 == $res || $res > 0) { unset($child[$k]); } } } echo 'end '.microtime(true).PHP_EOL;
當$total=10000,$num = 10;執行結果如下:
start 1491903371.5548 child 19860 finished 1491903417.2113 child 19857 finished 1491903417.6909 child 19864 finished 1491903417.7793 child 19855 finished 1491903417.8695 child 19859 finished 1491903417.9162 child 19861 finished 1491903418.0089 child 19856 finished 1491903418.0532 child 19863 finished 1491903418.0842 child 19862 finished 1491903418.1474 child 19858 finished 1491903418.4341 end 1491903418.4424 总时间为46.88759994506836秒
當$total= 10000,$num = 100時,執行結果如下:
start 1491904334.1735 child 20085 finished 1491904337.0712 child 20086 finished 1491904337.144 …… child 20262 finished 1491904341.5602 child 20264 finished 1491904341.5803 end 1491904341.5869 总时间为7.413399934768677
當$total=10000,$num = 1000時,執行結果如下:
start 1491904562.0166 child 20282 finished 1491904562.1191 child 20277 finished 1491904562.1268 child 20279 finished 1491904562.1352 ... child 21586 finished 1491904576.6954 child 21582 finished 1491904576.7024 child 21584 finished 1491904576.7226 end 1491904576.7297 总时间为14.71310019493103,相比100个子进程,耗时更长了。进程切换太多,影响了了效率应该是原因之一。
當$total=100000 ,$num =100時,十萬筆記錄,100個進程插入
start 1491905670.2652 child 21647 finished 1491905725.4382 child 21651 finished 1491905725.4595 child 21642 finished 1491905725.5402 .... child 21810 finished 1491905729.7709 child 21812 finished 1491905729.8498 child 21811 finished 1491905729.9612 end 1491905729.9679 总时间为59.70270013809204
單一進程插入1萬筆數據,耗時18秒,相對10個進程插入1萬筆記錄來說,耗時少一點。
而單進程插入10萬筆記錄,耗時187.40066790581,相對來說,是挺慢的了。三分鐘。 。 。
不過,本人再fork 1000個進程,來插入10萬記錄時,成功的情況下36秒左右,也可能會出現錯誤,mysqli_connection返回false,是不是連線數受限了?
fork 一萬個子進程,插入一百萬個數據,這時,出現連接錯的情況就很多了。最後耗時360秒,在資料表中插入了945,300筆記錄,成功率94.53%。於是查看資料庫的相關設定資訊
mysql> show global status like '%connect%'; +-----------------------------------------------+---------------------+ | Variable_name | Value | +-----------------------------------------------+---------------------+ | Aborted_connects | 0 | | Connection_errors_accept | 0 | | Connection_errors_internal | 0 | | Connection_errors_max_connections | 628 | | Connection_errors_peer_address | 0 | | Connection_errors_select | 0 | | Connection_errors_tcpwrap | 0 | | Connections | 16519 | | Locked_connects | 0 | | Max_used_connections | 501 | | Max_used_connections_time | 2017-04-12 15:19:54 | | Performance_schema_session_connect_attrs_lost | 0 | | Ssl_client_connects | 0 | | Ssl_connect_renegotiates | 0 | | Ssl_finished_connects | 0 | | Threads_connected | 4 | +-----------------------------------------------+---------------------+ mysql> show global variables like '%connect%'; +-----------------------------------------------+--------------------+ | Variable_name | Value | +-----------------------------------------------+--------------------+ | character_set_connection | utf8mb4 | | collation_connection | utf8mb4_general_ci | | connect_timeout | 10 | | disconnect_on_expired_password | ON | | init_connect | | | max_connect_errors | 100 | | max_connections | 500 | | max_user_connections | 0 | | performance_schema_session_connect_attrs_size | 512 | +-----------------------------------------------+--------------------+ 修改 myqsql 配置文件,/etc/my.cnf 把max_connections 改为10000,然后重启mysql 实际MySQL服务器允许的最大连接数16384; 结果然并卵,虚拟机好像挂了了。
並發量大的時候,問題就出在連接mysql這裡了。
可以透過一個連線池來嘗試解決該問題。
以上是php多進程插入數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP 8.4 帶來了多項新功能、安全性改進和效能改進,同時棄用和刪除了大量功能。 本指南介紹如何在 Ubuntu、Debian 或其衍生版本上安裝 PHP 8.4 或升級到 PHP 8.4

Visual Studio Code,也稱為 VS Code,是一個免費的原始碼編輯器 - 或整合開發環境 (IDE) - 可用於所有主要作業系統。 VS Code 擁有大量針對多種程式語言的擴展,可以輕鬆編寫

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

字符串是由字符組成的序列,包括字母、數字和符號。本教程將學習如何使用不同的方法在PHP中計算給定字符串中元音的數量。英語中的元音是a、e、i、o、u,它們可以是大寫或小寫。 什麼是元音? 元音是代表特定語音的字母字符。英語中共有五個元音,包括大寫和小寫: a, e, i, o, u 示例 1 輸入:字符串 = "Tutorialspoint" 輸出:6 解釋 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。總共有 6 個元

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

PHP的魔法方法有哪些? PHP的魔法方法包括:1.\_\_construct,用於初始化對象;2.\_\_destruct,用於清理資源;3.\_\_call,處理不存在的方法調用;4.\_\_get,實現動態屬性訪問;5.\_\_set,實現動態屬性設置。這些方法在特定情況下自動調用,提升代碼的靈活性和效率。
