I want to run a background php process(es) that does some work. I thought of two ways to doing this and I want to know which one is better and which is more possible using TideSDk
1) I've thought of using the Ti.Process.createProcess() in a js file to spawn off a process. The problem I faced with this is that there is no way to access the information recorded by this process without writing it to a file. Since Ti object is not available in a php script file i used the fopen() and fwrite() but these methods don't seem to be working. IS there any way I can access information from this process without trying to write to a file?
2) Another way I thought of doing this is to use javascript setInterval() and somehow connect it to the application and call the php function periodically. Is this possible to call a php function from a js file?
I want to maintain the js and php files separately and not have any inline scripting in the html files. Please let me know if I could do it with any of the above or in any different way
Either use a cron job. Or php exec() function.
re: option 1 - you can open a socket between your app and your process. Thats how I do it (in my case running bg ruby script).
Related
I'm building small application to collect data. For data collection PHP is used, for data storage PostgreSQL is used. PostgreSQL is included so I have full control over it. The PHP for collection is triggered by external entity and I have no control over PHP interpreter that will run the code.
Is there a way how to load php_pgsql.dll? at run-time?
I know it was asked already, for example here, here and my best source of information was here. If I get it right there is no way if I'm not root of the system (because dl() was removed).
I can add PHP to my application the same way I have added PostgreSQL (to have control over PostgreSQL and do not need to ask someone to install, configure, maintain...), BUT my PHP files are triggered by external application so I have no control over used PHP interpreter/environment.
Is there a way to start from PHP code (let's call it systemPHP) the same PHP code but in different PHP environment (myPHP environment I have control over and where I will have the dll included)?
For example if systemPHP starts collect.php the pseudo code of collect.php will be:
if <this is myPHP> { # How to detect it?
<execute the data collection code>
}
else {
<Start collect.php in myPHP transfering all the data to it> # For example if started by apache then also headers, session information etc...
<Send back result from myPHP via the systemPHP>
}
How to achieve this PHP 'tunnel'?
Thanks for any help or hint. I know that best will be root or at least have intelligent admin, however this is not the case :-(
Currently I'm trying workaround by executing database tasks via shell and then getting response back in PHP, but sometimes it works sometimes not and I believe there is a better way of doing this (not to mention speed and resource usage).
Have you looked into using a messaging queue system? Write to the queue, then have your PHP script running that has php_pgsql.dll already loaded, which checks for new messages in the queue and processes them.
After asking this question, someone pointed on the right direction of not being able to execute a second script at all if one was already running.
I usually make apps which rely on the execution of AJAX calls to PHP pages, and today I found that trying to write on a file with fwrite() on a PHP script and trying to read that same file with fread() (to get progress feedback) on another AJAX call ended up in the second script only being executed when the first one had already finished.
Even trying to echo a simple "hello" (echo "hello"; exit;) would not show nothing on the page until the first script was finished.
So, I'm asking: is this a normal configuration? Is this the same on every installation of PHP by default? Is some configuration on php.ini that I can change?
Or it has to do with the server (in my case, Microsoft IIS 10)? Can someone shed some light on how to be able to execute multiple PHP scripts on different AJAX calls at once (or before the others finish)?
I know I'm not giving much information about the settings of my context, but I don't know neither where to look into.
Thank you everyone for your time and help!
As Luis said it could be a write-lock on the file that you're trying to modify. However another possibility if you're using sessions that use files (rather than a database), or a framework that uses file-based-sessions - then this behavior could also be a result of session-locking. My money would be on Luis' answer though - you should probably be using a database rather than a file unless you have a solid reason not to.
Not sure if I understood the use/purpose of PHP entirely, but what seems to me that a .php file only executes when it is being called/executed by something before it, could be a html or a php file.
A thought, is it possible that a php file written, and it would just be activated by its own, example over a duration span of time, every week or so it would do something?
Thanks for looking...
You are looking for a cron job. This allows you to save a line of code on your remote server that will execute based on the criteria you set. You can make it execute a variety of files but PHP files are definitely one of the files you can execute in this manner.
As mentioned by nathan, you will be looking for a cron job. This is a server side setting in the server that will call a url at a set interval.
You seem to not really understand how PHP works. PHP scripts are called server-side before sending data to the client. They are run once when the client is accessing the script.
what my page do is:
download an array from different server (first.php)
php script parse values
parsed values are sent with ajax call
on the next (ajax called) page (second.php) there are some mysql queries
if values pass condition, values are written to database
.... So, when I run my first.php.. it loads second.php, everything's fine..
but what I want to know if it is possible to let it make by cron?
If not, what should I do?
Thanks.
There are certain things you need to understand in this regard.
The first is that PHP can be run as either a web server module or as a standalone executable. When you run it as a web server module, you open it from the browser, all related web technologies (html/css/js) etc get parsed and work in unison.
When you run it from command line using cron like say /usr/bin/php mywebpage.php
then the php executable DOES NOT parse/understand the other web technologies and so your page will fail.
There are two workarounds for this:
Rewrite only those web-enabled parts so that the ajax/js stuff gets
handled by PHP. Basically rule of the thumb is that if you are
running a CLI php script, it should contain ONLY core PHP. This is the preferred way. You will need to move the ajax calls to inside the same file and just make it a single execution flow like any regular program.
If for some reason you cannot do the above, you can try something like this:
/path/to/browser http://mysite/mywebpage.php. Here what you are doing is, you are running a browser executable and then calling the webpage URL. This way the page is being executed within the browser's environment and it will be able to parse and understand the ajax/js calls.
Yes you can create a cron job in the below way.
1) download an array from different server (first.php)
2) php script parse values in first.php
3) Include the second file, second.php by include_once which executes mysql queries
4) If everything is correct insert them to database.
It sounds like you need a standalone JavaScript shell. There are a number listed at:
https://developer.mozilla.org/en-US/docs/JavaScript/Shells
I'm trying to integrate an old PHP ad management system into a (Django) Python-based web application. The PHP and the Python code are both installed on the same hosts, PHP is executed by mod_php5 and Python through mod_wsgi, usually.
Now I wonder what's the best way to call this PHP ad management code from within my Python code in a most efficient manner (the ad management code has to be called multiple times for each page)?
The solutions I came up with so far, are the following:
Write SOAP interface in PHP for the ad management code and write a SOAP client in Python which then calls the appropriate functions.
The problem I see is, that will slow down the execution of the Python code considerably, since for each page served, multiple SOAP client requests are necessary in the background.
Call the PHP code through os.execvp() or subprocess.Popen() using PHP command line interface.
The problem here is that the PHP code makes use of the Apache environment ($_SERVER vars and other superglobals). I'm not sure if this can be simulated correctly.
Rewrite the ad management code in Python.
This will probably be the last resort. This ad management code just runs and runs, and there is no one remaining who wrote a piece of code for this :) I'd be quite afraid to do this ;)
Any other ideas or hints how this can be done?
Thanks.
How about using AJAX from the browser to load the ads?
For instance (using JQuery):
$(document).ready(function() { $("#apageelement").load("/phpapp/getads.php"); })
This allows you to keep you app almost completely separate from the PHP app.
Best solution is to use server side includes. Most webservers support this.
For example this is how it would be done in nginx:
<!--# include virtual="http://localhost:8080/phpapp/getads.php" -->
Your webserver would then dynamically request from your php backend, and insert it into the response that goes to the client. No javascript necessary, and entirely transparent.
You could also use a borderless <iframe>
I've done this in the past by serving the PHP portions directly via Apache. You could either put them in with your media files, (/site_media/php/) or if you prefer to use something more lightweight for your media server (like lighttpd), you can set up another portion of the site that goes through apache with PHP enabled.
From there, you can either take the ajax route in your templates, or you can load the PHP from your views using urllib(2) or httplib(2). Better yet, wrap the urllib2 call in a templatetag, and call that in your templates.