V
V
Vladimir Chernyshev2012-12-29 20:22:06
PHP
Vladimir Chernyshev, 2012-12-29 20:22:06

Interaction of a PHP application with a C module. How is it "correct"?

Current situation: There is an application in PHP where one of the existing modules cannot cope with the load (calculation of the interaction of tens of thousands of objects in several - up to tens - iterations does not fit in no more than a few seconds). A decision was made to rewrite it in C, an open module was found for a similar project, where the interaction goes by creating a text file with the initial task data, passing the file name in the exec parameter, and issuing serialized results to the same file. Processing is conditionally synchronous - the user receives a response with a handled event if the event occurred at the time of the GET request or receives an empty response. PHP is responsible for determining the time, preparing the input and processing the output. All data is taken from the database (MySQL), placed there (partially in html - report), in C only heavy calculations.
There are ideas to write a daemon listening on a socket with which php will communicate, sending tasks and receiving a response, or using something like Gearman / RabbitMQ, in which php will put tasks, a C daemon will take and process, and php will wait for a response. You can also write a module in PHP by introducing the necessary functions or classes directly into PHP in order to score on interprocess communication in general, but I can’t even imagine the approximate laboriousness.
In principle, you can get rid of synchronism by receiving the responses “event processing has begun” and “processing has ended”, by making an auto-update on the client.
Can someone suggest a better option? Or is it just to score and leave until the real exec and work with the FS become a bottleneck?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
schroeder, 2012-12-30
@schroeder

look towards SWIG . It works for us, everyone is happy. No databases, no file reads.

R
Rowdy Ro, 2012-12-29
@rowdyro

The most correct way out is to write a daemon, communicate with it from php via tcp, for example, through protobuffers.
This approach will take you away from the problem with horizontal scaling, when the local machine will not have enough resources, and it will be possible to distribute tasks to demons by round-robin, which can be an unlimited number.
Such a scheme of work for many systems - sphinx for example.
There is also fastcgi, you can generally make your sish daemon work without php - nginx will help you.

R
romy4, 2013-12-31
@romy4

for example using fastcgi.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question