简体   繁体   English

PHP多线程

[英]PHP multi-threading

I want to perform multiple for loops at once.我想一次执行多个 for 循环。 For loop consists of socket operations. For 循环由套接字操作组成。 I want to send request and get response using socket.我想使用套接字发送请求并获得响应。 Current problem is that first of all First for loop executes and then second one.当前的问题是首先执行第一个 for 循环,然后执行第二个。 How do I perform simultaneously ?我如何同时执行? Please explain with example.请举例说明。 This is the code :这是代码:

<?php

for($i=0;$i<1000000;$i++)
{
  //Soecket operations
}  

for($i=0;$i<1000000;$i++)
{
  //Soecket operations
}  

for($i=0;$i<1000000;$i++)
{
  //Soecket operations
}  

for($i=0;$i<1000000;$i++)
{
  //Soecket operations
}  
for($i=0;$i<1000000;$i++)
{
  //Soecket operations
}  
for($i=0;$i<1000000;$i++)
{
  //Soecket operations
}  
for($i=0;$i<1000000;$i++)
{
  //Soecket operations
}  
for($i=0;$i<1000000;$i++)
{
  //Soecket operations
}  
for($i=0;$i<1000000;$i++)
{
  //Soecket operations
}  

?> ?>

How can I perform all of them at once ?我怎样才能一次执行所有这些?

Thank you!谢谢!

Сreate bash script with calls to php scrips inside.创建 bash 脚本并调用里面的 php 脚本。

bash-start.sh bash-start.sh

#!/usr/bin/env bash

    /bin/ps -auwx | /usr/bin/grep  custon-name-php-file.php | /usr/bin/awk  '{print $2}' | /usr/bin/xargs kill -9

    for i in `seq 1 15`;  # 15 - count threads
    do
       echo $i
        /usr/sbin/daemon custon-name-php-file.php $i
    done

custon-name-php-file.php自定义名称-php-file.php

#!/usr/local/bin/php
<?php

$thread = $_SERVER['argv'][1];

for($i=0;$i<1000000;$i++)
{
  //Soecket operations
  echo $thread."). ".$i.PHP_EOL;
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM