create multiple instances of a script

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
PHPPadawan
New php-forum User
New php-forum User
Posts: 4
Joined: Sun May 20, 2018 10:30 am

Fri Jun 15, 2018 5:15 am

Hey guys,
I have 40 users that I’d like to run a script for – the script takes about 10 minutes to process each user so it takes over 6.5 hours to run.
I really wanted it to take an hour or less so I copied the script 7 times and divided the users across the 7 scripts – that worked but is there a way that I can have one script that creates a new instance for say every 6 users?

chorn
php-forum Fan User
php-forum Fan User
Posts: 559
Joined: Fri Apr 01, 2016 2:18 am

Fri Jun 15, 2018 10:21 am

You can run every file over CLI via `php -f file.php`, or use thread with e.g. pthreads.

PHPPadawan
New php-forum User
New php-forum User
Posts: 4
Joined: Sun May 20, 2018 10:30 am

Sat Jun 16, 2018 1:12 pm

thanks for the reply.
Unfortunately, pthreads looks too complex for me to use at the moment - it's something I'll definitely consider once I've got a better grasp of php.
I’ve created a script that’ll split the users into groups - is there any way I can create a new CLI instance and pass it a mysqli object containing that groups usernames and passwords

Code: Select all

<?php
 $connect = mysqli_connect("localhost", "root", "", "users_db");  
 $query ="SELECT * FROM users";
 $result = mysqli_query($connect, $query);

 
 $usercount = mysqli_num_rows($result);
 
 
 $totalInstances = $usercount / 5;
 for ($i = 0; $i <= $totalInstances; $i++) {
 $offset = $i * 5;
 $query = "SELECT * FROM users ORDER BY user_id LIMIT $offset,5";
 $result = mysqli_query($connect, $query);
  while($row = mysqli_fetch_array($result)) { 
  
		echo "User: ".$row['user_id']."<br />";
 } 
  
   echo "..................... ^ Create a new CLI instance to process this group of users <br />";
  
} 

Code: Select all

-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jun 16, 2018 at 10:03 PM
-- Server version: 10.1.30-MariaDB
-- PHP Version: 7.2.1

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `users_db`
--

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `user_id` int(3) NOT NULL,
  `user_password` varchar(16) DEFAULT NULL,
  `user_name` varchar(12) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`user_id`, `user_password`, `user_name`) VALUES
(1, '9pqf6B4ge', 'Tamara'),
(2, '5eFN4B', 'Ariel'),
(3, '7DLSsLrdBY', 'Katha'),
(4, '72LhHJwA0U2', 'Ilyssa'),
(5, 'BSGKlaDkqmO', 'Moreen'),
(6, 'r0fsxx2ltzi8', 'Marchall'),
(7, '7A0hlt', 'Dianne'),
(8, 'M0SzOzhZLUvk', 'Corrie'),
(9, 'Q5MTvpk4V', 'Harvey'),
(10, 'VmMruG', 'Auria'),
(11, 'IboWcV0hqwFT', 'Marcelle'),
(12, 'mhDhIBC', 'Patrizia'),
(13, 'Pes17rv', 'Berthe'),
(14, 'dQM3Z2190o', 'Cam'),
(15, 'kGRGZV0tKL', 'Brooke'),
(16, 'rT8vvY', 'Marquita'),
(17, '7n6mReA', 'Tadeo'),
(18, 'MjNS06AAX', 'Zena'),
(19, '0zhb05GQ', 'Hashim'),
(20, 'S2eEMSfJsH', 'Fifine'),
(21, 'TDnGHIP', 'Gavin'),
(22, 'Y4ku0yK1iWl', 'Ilysa'),
(23, 'm8kNlOt8ZMw', 'Cassandry'),
(24, 'DhpNLsq9', 'Adela'),
(25, '22k9iNh', 'Analiese'),
(26, 'XyjoTSn2d5f', 'Maure'),
(27, 'KiepflrUV', 'Briant'),
(28, 'Pzff44upD0Sg', 'Benjie'),
(29, 'SyrVR1d2Eor0', 'Margeaux'),
(30, 'SR2ml3vT5uHT', 'Pepe'),
(31, 'SqaXVqzWrUxS', 'Nicol'),
(32, 'Lz0WWU1qHZ', 'Patrice'),
(33, 'LQXaW0gv', 'Gram'),
(34, '1qwNmNvwL5N', 'Erskine');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`user_id`);
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


Post Reply