3
I would like to know if there is a way to create a process queue.
For example, whenever I have the file run processar.php
, if it is already running, it would wait to be executed after the end of the other. Only running when it is "free";
3
I would like to know if there is a way to create a process queue.
For example, whenever I have the file run processar.php
, if it is already running, it would wait to be executed after the end of the other. Only running when it is "free";
6
That seems very simple.
According to what you described in the question, I don’t think you need threads, queues, etc. Just create a flag.
When to run processo.php
create a flag that identifies you are running.
Example:
/*
Coloque no início do arquivo, antes de qualquer execução.
*/
if (!file_exists('processo.txt')) {
// levanta flag
file_put_contents(`processo.txt`, 1);
} else {
exit; // está em execução
}
/*
aqui faz os paranauê , etc e tal
*/
/*
aqui é o final, quando terminal tudo , remove a flag
*/
unlink('processo.txt');
A simple tip to improve is to compare the date and time the file was created. If the process lasts on average 10 minutes and the flag file next runs for more than 10 minutes, then something wrong or inexperienced has occurred. What we do in this case is our own choice. You can automate with an email alert, or you can just ignore and delete the file and generate a new process, generate log, anyway. I cannot go into these details because it depends on the business model of each and, trying to explain here would make the response very extensive and tiring, dispersing the focus.
I believe a solution should be simple, efficient, readable and portable before it is elegant.
The above example shows as simple and portable as possible.
It is portable in the sense that you can use it under any environment (linux, windows, mac) and even on a hosting server with limited resources such as shared hosting.
Resource consumption is minimal, much faster than generating a flag in a database.
You can also choose to generate the flag on the Operating System environment variables, including linux systems have features like Posix. However here we are already creating a complication that in most cases is unnecessary.
Usually if you use something more robust, pq if the machine happens to restart with the already created file (or even if there is an error in the script before the file is deleted), PHP will no longer run. Perhaps a non-permanent environment variable would be better, something that couldn’t resist a reset. Remembering that the author is probably trying to solve the problem in the wrong place (it would probably be much simpler, for example, to use a crontab with shell script to run PHP once, for example)
Yes, there are several ways to solve it. I thought it was unnecessary to demonstrate a dazzling and "perfect" technique for a question not very clear. I believe that what I have presented is sufficient for a general purpose, for something amateurish, simple as described in the question. Usually a technician focuses too much on a "perfect" solution and often there is not even a real need. Can solve more appropriately just by observing the business model.
I just thought it was important to mention, because this file being "locked" holding the application is very common, maybe it’s nice after you mention it only as an alert (until pq later we can delete the comments from here); PS: the other answer suffers from the same problem
Each one chooses which way to go, which rule to adopt in the event of a problem, a lock for example. Sometimes in a situation where the system hangs and keeps the flag active, it can be a useful resource to prevent new processes from being triggered because a system failure may indicate other problems that may affect the next executions of the process in question, can cause bigger problems. Therefore, there is no way to determine/state what is appropriate or not. But of course, it is valid to warn about the consequences.
Daniel Omine, I did exactly this way, adapted to my need, at first I found the action of "write and delete" bad, inelegant file, but this way solved the problem.
2
I believe that using thread It won’t work for what you want, because according to my research, thread is a feature that will allow you to run in parallel blocks, so I understand you want sequential execution of code blocks. See what this link talks about threads:
What are threads?
Before answering this question, it must be said that there are two thread types: Kernel Level Thread (KLT) and User Level Thread (ULT). In this article, we will focus only on the ULT threads, which are supported by PHP.
An ULT is a set of instructions that can be executed in parallel with other instructions from the same program. This way, when we have a device (computer) with multiple processors or a processor with multiple cores (multi-colors), it is possible to place two (or more) threads of the same process running in parallel, each in one own processor or core. The result of this is a potential improvement in process performance. That is, instead of running instruction by instruction sequentially, you can perform some instructions blocks in parallel and, at certain points in the code, require a synchronization to ensure that the "results" or processing performed by the parallel tasks have been completed.
One important feature to highlight is that the threads share the same memory region as the process that started it. Therefore, several threads can work on the same data from memory. By being part of a single process, use threads usually performs better than running multiple processes equal in parallel. On the other hand, threads need to have some kind of control over access to the same data, since there may be access competitor.
Use Session as the friend suggested can resolve, if your concern is waiting for the "release" of the file processa.php
per user, since a Session is created for each user. That is, you cannot block the execution of the php file to a user if another is running it.
My suggestion is that you try to resolve this by using the database, creating an execution queue. You will need a PHP script, run asynchronously, which will take from the database the next request to be processed and will call the processa.php
.
In the database, you can create columns to store the relevant variable information or even the $_POST you would later use.
0
A while ago I had to interact php with a pipeline written in bash with many steps and that called many python and R functions sequentially. So I thought of the whole process as sub-processes where Process 2 was only started when Process 1 ended, Process 3 after Process 2 and so went through the last process. I think that’s what you’re looking for right?
The solution is to use this combination in an imbricated way:
...
//Processo-1 iniciou a reação... e gerou um arquivo => fileResultProcesso-1.txt
...
$control-2=true;
while($control-2){
if(file_exists(/path/fileResultProcesso-1)){
//Se o arquivo xiste é pq o processo-1 já terminou.
//Entáo inicie o processo-2
//Salve o resultado do processo-2 (ou uma tag qualquer)
//COloque $control-2 = False
//////////////////////// -- Início do processo-3
$control-3=true;
while($control-3){
if(file_exists(/path/fileResultProcesso-2)){
... continue até terminar seus processos ....
}//Fim do if file processo-1
}//Fim do processo-3
}//Fim do if file processo-1
}//Fim do processo-2
Note: Regarding breaking the process in the middle of the way. You have to make a decision. You will want to restart the whole process or will continue from where you left off?
My algorithm deleted all the files and folders generated at the end of the process. But I could save all to restart from where I left off and make a small change to the code: check if the file or its tag exists. If yes go to the next step.
Browser other questions tagged php process queue thread-queue
You are not signed in. Login or sign up in order to post.
The name of this is thread, yes it exists, look here and here too
– Ivan Ferrer
Utilize Session and thread can be tbm, follow tutorial http://php.net/manual/en/ref.session.php ... used only Session is simple working web.
– KingRider
In my view the wig is clear. He wants to know how to create a stack of processes.... senquenciamento.
– zwitterion
I am reopening the question, but you do have clarity issues. Could you explain how you are running the file? Is it by command line? It’s for an HTTP request?
– bfavaretto