T
T
topuserman2021-10-22 17:29:30
go
topuserman, 2021-10-22 17:29:30

Theoretical question: is it possible to run php scripts and how correct is this approach?

Is it possible to write a script that will listen on a specific port and receive messages.
When receiving each message, you need to run a php script and pass it a parameter (via args) with the message data.
You do not need to wait for the completion of the php script.
If several messages have arrived at once, then run them in parallel, as separate processes.

Why do I need it?
It is often necessary to perform some heavy tasks in php scripts.
for example, the user has started re-indexing a directory.
And I would like the reindexing itself to start immediately, but as a separate process, so as not to slow down the current script.

In general, now I solve such problems with a queue server.
I add a message to the queue, and the consumer picks up and performs these tasks.

Due to one small task (launching tasks in the background), you have to install heavy queue servers.

It seems to me that if you write such a script, you can solve this problem, but using one small script.

I would like to know the opinion of go-developers, what problems can be encountered, and how effective is this approach?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Shitskov, 2021-10-22
@Zarom

Queues and limited number of consumers solve one useful problem - if a user requests reindexing 50 times, more than N processes will not work at the same time and these 50 requests will not lead to collapse.
If classical mq is difficult for such tasks, then you can use something easier - NATS or even Redis.
And so you can run scripts with anything, but the approach is very, very not very good. But to be honest, there is something worse - I saw a project in which php scripts generated and ran shell scripts.

R
Roman Mirilaczvili, 2021-10-23
@2ord

If I understand correctly, the scheme is as follows:
Producer (1st PHP script) -> MQ broker -> Consumer (2nd PHP script)
This is a common micro-service architecture. The advantage of each service is that each of them does not store state. That is, the producer is "not interested" in the fate of the sent task, and he does not keep any record of what and to whom the task was sent.
If you do not want to take heavyweight message queue brokers, then you can take it as advised by Dmitry Shitskov or even ZeroMQ.
The same task can be solved by creating a new thread in the main thread of the program and waiting for the end of execution of the thread executing the task. In the case of Go, instead of a flow, a goroutine is created (just a more lightweight version of the flow) - the meaning is the same.
In order not to heavily load the system, a limited number of consumer threads are used.
Added
In the case of Go, if you do not need to wait for the end, then you can send a task to the channel. Channel consumers must pick up jobs, otherwise the channel may block (when the channel's task buffer fills up or on each new task, depending on the channel's configuration).

A
artloveyou, 2021-10-22
@artloveyou

If the php script is a console script, what's the difference than running it. You can also go.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question