Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement multithreading in php

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to implement multithreading in php". The content is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "how to implement multithreading in php".

Public function testa () {

$id = $_ REQUEST ['id']

For ($iAdd ($data)

}

Public function testb () {

$id = $_ REQUEST ['id']

$time = time ()

$data ['time'] = $time

M ('test')-> add ($data)

}

Php Bensheng does not support multithreading. If you want the above two methods to execute at the same time, I use curl. Here is the code for curl parallel these two methods.

This section is hidden, you have replied, the following is the hidden content

/ / load a multi-process CURL instance

$mh = curl_multi_init ()

$handles = array ()

$array=array ("localhost:1001/index.php/test/testa", "localhost:1001/index.php/test/testb")

Foreach ($array as $k = > $v)

{

/ / create a single-threaded CURL instance

$ch = curl_init ()

/ / set parameters related to CURL

Curl_setopt ($ch, CURLOPT_URL, $v)

Curl_setopt ($ch, CURLOPT_HEADER, 0)

Curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true)

Curl_setopt ($ch, CURLOPT_TIMEOUT, 30)

/ / load the process into the instance

Curl_multi_add_handle ($mh,$ch)

/ / add to the circular array

$handles [] = $ch

}

/ / execute CURL multithreaded instance

$running=null

Do

{

Curl_multi_exec ($mh,$running)

/ / interval 0.25s

/ / usleep (250000)

} while ($running > 0)

/ / obtain the collection content

For ($iSuppli

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report