Bash loop script PHP with auto increment variable - php

I'm trying to do a Bash script like:
n=1; While(n<=500) php /Path/script.php?v=$n ++n;
I would like to these script run in parallel. It could use so much of CPU? Or maybe parallel wouldn't be good?

A simple bash solution is to use the for construct and send the current instance of said script to the background, like this:
for n in {1..500}; do
php /Path/script.php?v=$n &
done
This will make it so that it's ran 500 times in parallel, whether it's going to use up all of your resources or not depends on what resources are available and what the script does.
I for one, doubt that this is an ideal solution, but it depends on what you're trying to achieve and what the reason for wanting to do it this way is.

Related

Long PHP Response end with Returning Query String

We have a query that takes about 10 minutes to complete but it seems that PHP doesn't want to wait around for it. If the user does, PHP will respond with the actual query in plain text. No errors or anything like that are reported.
We thought it was an Apache thing, tried to configure it, didn't work, swapped over to nginx and it still gets the same result.
I call set_time_limit(0); at the very beginning of the script.
What is it I'm missing?
EDIT:
I'd like to clarify that PHP isn't doing any computation. It's simply waiting for the result of a query from the database and then formatting the result into an Excel file.
Try setting "max_execution_time" with
ini_set("max_execution_time", 0); but PHP was not build for this kind of work - try to use different dev language (Python, NodeJS?)
Did you try to make your query more optimized? Or try running it in more steps. Try generating some tables with partial results. Then re-do the query on those?
Unfortunately php is not built for heavy processing
I also had this problem in some of my code
My suggestion is:
Use of micro-service structure (nodeJs, Java ,etc.)
Use queuing and split processing into smaller sections
But in general, it is better to entrust heavy processing to another programming language
Like Java,python, ect
You can read more about this on these sites:
https://www.reddit.com/r/PHP/comments/2pyuy0/heavy_data_processing_in_php/
Best way to manage long-running php script?

re-compile from source to include setInterval functionality?

I have a very troubling problem at hand. I am using a web-socket server that runs in PHP. The issue is I need to be able to use a setInterval/setTimeout function similar to JavaScript, but within my php socket server.
 
I do not have the time or resources to convert my entire project over to nodejs/javascript. It will take forever. I love php so much, that I do not want to make the switch. Everything else works fine and I feel like it's not worth it to re-write everything just because I cannot use a similar setInterval function inside php.
 
Since the php socket server runs through the shell, I can use a setInterval type function using a loop:
http://pastebin.com/nzcvXRph
 This code does work as intended, but it seems a bit overboard for resources and I feel like that while loop will suck a lot resources.
Is there anyway I can re-compile PHP from source and include a "while2" loop that only iterates every 500 milliseconds instead of instantly?
I don't think there is a way to recompile PHP from source.
If you want to delay the execution of the loop you could use the sleep function, which is used for delaying execution.
For example, I want to print 10 number after every 2 seconds then the code below should do the job.
for($i=0;$i<=10;$i++)
{
print($i++);
sleep(2);
}
Check thee PHP docs here.
EDIT
Following up what I mentioned in the replies, if you want the user to have its own instance of the run time, then threads would be an option. There is very limited examples of multi threaded application in PHP, I would recommend to check out some examples in JAVA, it shouldn't he hard to understand. Here is a good video tutorial.
For PHP
php.net/threads
Check out the contributor notes, sometimes people write good examples.

php waiting between two operation (multi-threading)

I have code structure like this
echo "First Operation";
sleep(3);
echo "Second Operation";
I want to try simulate multi-threading but I could not make, sleep function is meaningless there, but I could not find another option.
I have to agree that PHP is not built for multithread. As such the script you create will always run in one thread. There are some exceptions to that. When you call a shell command there is the possibility to fire-and-forget the command when you dont need the output. It runs parallel but you can not access the result.
PHP is designed to execute one instruction after the other.
To create something like a parallel working script, you need to take advantage of some external systems. All of those tricks are related to your actual solution you wane try to accomplish. Let me give you an example.
You could use a Message Queueing system like Rabbit MQ to work as a separation between one script and the other. Lets say script1 is permanently listening on the queue for work to do. Another script, lets say script2, is (on request) bushing a work into the queue and continuing with other stuff while the script1 is picking that up, doing something and returning the answer. When script2 is done with the other stuff it can read the result from the queu and finish whatever you want to do.
This is actually a very tricky and theoretical concept, but it might give you an idea. But without any external component like the queue, php will not work multithreaded.
I hope that is helping you!

How can I make a scheduler in PHP without the help of cron

How can I make a scheduler in PHP without writing a cron script? Is there any standard solution?
Feature [For example]: sent remainder to all subscriber 24hrs b4 the subscription expires.
The standard solution is to use cron on Unix-like operating systems and Scheduled Tasks on Windows.
If you don't want to use cron, I suppose you could try to rig something up using at. But it is difficult to imagine a situation where cron is a problem but at is A-OK.
The solution I see is a loop (for or while) and a sleep(3600*24);
Execute it through a sending ajax call every set interval of yours through javascript
Please read my final opinion at the bottom before rushing to implement.
Cron really is the best way to schedule things. It's simple, effective and widely available.
Having said that, if cron is not available or you absolutely don't want to use it, two general approaches for a non-cron, Apache/PHP pseudo cron running on a traditional web server, is as follows.
Check using a loadable resource
Embed an image/script/stylesheet/other somewhere on each web page. Images are probably the best supported by browsers (if javascript is turned off there's no guarantee that the browser will even load .js source files). This page will send headers and empty data back to the browser (a 1x1 clear .gif is fine - look at fpassthru)
from the php manual notes
<?php
header("Content-Length: 0");
header("Connection: close");
flush();
// browser should be disconnected at this point
// and you can do your "cron" work here
?>
Check on each page load
For each task you want to automate, you would create some sort of callable API - static OOP, function calls - whatever. On each request you check to see if there is any work to do for a given task. This is similar to the above except you don't use a separate URL for the script. This could mean that the page takes a long time to load while the work is being performed.
This would involve a select query to your database on either a task table that records the last time a task has run, or simply directly on the data in question, in your example, perhaps on a subscription table.
Final opinion
You really shouldn't reinvent the wheel on this if possible. Cron is very easy to set up.
However, even if you decide that, in your opinion, cron is not easy to set up, consider this: for each and every page load on your site, you will be incurring the overhead of checking to see what needs to be done. True cron, on the other hand, will execute command line PHP on the schedule you set up (hourly, etc) which means your server is running the task checking code much less frequently.
Biggest potential problem without true cron
You run the risk of not having enough traffic to your site to actually get updates happening frequently enough.
Create a table of cronjob. In which keep the dates of cron job. Keep a condition, if today date is equal to the date in the creonjob table. then call for a method to execute. This works fine like CRON job.

run php script after every 100ms

Is it possible to run a php script after every 100ms ? This script will check a database for changes and then the changes will be reflected into some other database. I am already doing it using Triggers. But I want to know if there is any other way to do it without using Cron and Triggers. I will be using Linux for this purpose.
Thanks
Running something every 100ms almost means that it runs all the time , might as well create a daemon that continuously loops and executes
or use triggers. Essentially on every database change it will copy to another table/db.
http://codespatter.com/2008/05/06/how-to-use-triggers-to-track-changes-in-mysql/
It is not possible to do this with cron (it has a max frequency of one minute) and this is a really bad idea. You will be running a whole new php interpreter ten times per second, not to mention doing database connection too.
Far better perhaps would be to run one program that re-uses it's connection and checks every second or so.
Sounds a little like you are trying to make your own database replication or sync between two databases.
You could write a daemon to do it, essentially a script which continually runs in memory somewhere to then run whatever code you want to.
So that daemon would then do the database processing for you, and you wouldn't have to call a script over and over again.
Use your favorite programming language and set up a permanent loop to run it every 100ms, then put the script into inittab with 'respawn' (man inittab for complete syntax). Finally, init q to reload init.
It's best if you write a little daemon for that. Use the pcntl functions to do so. In your case you might get away with:
<?php
while (1) {
usleep(100000);
if (pcntl_fork() == 0) {
include("/lib/100ms-script.php");
exit;
}
// else pcntl_wait(); eventually
}
I'm assuming that this is in reference to some type of web page to be created. If so, this sounds like this is a job for Ajax, not PHP. As you may already know PHP processing is done on the server side. Once processing is complete the page is served up to the client.
With Ajax/JavaScript processing can continue via the browser. You can setup a timer that can then be used to communicate with the server. Depending on the output of the response the page may be updated to reflect the necessary changes.

Categories