Home > Backend Development > PHP Tutorial > pthreads multi-threaded data collection_PHP tutorial

pthreads multi-threaded data collection_PHP tutorial

WBOY
Release: 2016-07-13 10:42:07
Original
1342 people have browsed it

The multi-threading that used curl in the past was not a real multi-threading, it was just a simulated multi-threading. Now pthreads is used to achieve real multi-threading.

Download:

Under windows:

 http://windows.php.net/downloads/pecl/releases/pthreads/0.0.45/

Under mac, unix, linux:

 https://github.com/krakjoe/pthreads

Under windows:

 Extract the pthreadVC2.dll and php_pthreads.dll files, put the vc2 file into the same directory as php.exe, and put php_pthreads.dll into the extension directory.

Modify the php.ini file and add extension=php_pthreads.dll

Modify the Apache configuration file httpd.conf and add LoadFile "yourpath/php/pthreadVC2.dll"

Under mac, unix, linux:

For details, please refer to Brother Yan’s blog http://blog.s135.com/pthreads/

Calling method:

For specific usage, you can also refer to Brother Yan’s blog http://blog.s135.com/pthreads/

Combined with the previous get_html, the class

can also be implemented in this way
<span  1</span> <span class</span> threads <span extends</span><span  Thread
</span><span  2</span> <span {
</span><span  3</span>     <span public</span> <span $url</span> = ''<span ;
</span><span  4</span>     <span public</span> <span $options</span> = <span array</span><span ();
</span><span  5</span>     <span public</span> <span $data</span><span ;
</span><span  6</span> 
<span  7</span>     <span public</span> <span function</span> __construct(<span $url</span>, <span $options</span> = <span array</span><span ()){
</span><span  8</span>         <span $this</span>->url = <span $url</span><span ;
</span><span  9</span>         <span $this</span>->options = <span $options</span><span ;
</span><span 10</span> <span     }
</span><span 11</span> 
<span 12</span>     <span public</span> <span function</span><span  run(){
</span><span 13</span>         <span if</span>(!<span empty</span>(<span $this</span>-><span url)){
</span><span 14</span>             <span $this</span>->data = <span $this</span>->get_html(<span $this</span>->url, <span $this</span>-><span options);
</span><span 15</span> <span         }
</span><span 16</span> <span     }
</span><span 17</span> 
<span 18</span>     <span public</span> <span function</span> get_html(<span $url</span>,<span $options</span> = <span array</span><span ()){
</span><span 19</span>         <span if</span>(<span empty</span>(<span $options</span><span )){
</span><span 20</span>             <span $options</span>[CURLOPT_RETURNTRANSFER] = <span true</span><span ;
</span><span 21</span>             <span $options</span>[CURLOPT_TIMEOUT] = 5<span ;
</span><span 22</span> <span         }
</span><span 23</span>         <span $ch</span> = curl_init(<span $url</span><span );
</span><span 24</span>         curl_setopt_array(<span $ch</span>,<span $options</span><span );
</span><span 25</span>         <span $html</span> = curl_exec(<span $ch</span><span );
</span><span 26</span>         curl_close(<span $ch</span><span );
</span><span 27</span>         <span if</span>(<span $html</span> === <span false</span><span ){
</span><span 28</span>             <span return</span> <span false</span><span ;
</span><span 29</span> <span         }
</span><span 30</span>         <span return</span> <span $html</span><span ;
</span><span 31</span> <span     }
</span><span 32</span> }
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/667912.htmlTechArticleThe multi-threading that used curl in the past was not a real multi-threading, it was just a simulated multi-threading. Now we use pthreads To achieve true multi-threading. Download: under windows: http:...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template