简体   繁体   中英

Update xml file every minute on server

I have many a function in python, that takes in the hours of operation for a store and outputs if, the store is open, closed or is closing in 30,29,28... minutes. Now I have an iOS and android app that will display the output for 100's of stores.

And I was thinking of having it do this by having my py script generate an XML file that says for each store whether it is open, closed or closing that would look something like this

<?xml version="1.0" ?>
<root>
    <name store="one">
        <field1 name="blah">open</field1>
    </name>
    <name store="two">
        <field2 name="slah">closing in 17</field2>
    </name>
</root>

But to do this I would have to have my py script run with cron jobs every minute, which can be intensive for the number of sore I will have this running for, and when the script is updating a new file, the old one goes down for about five seconds (depending on how long the script runs for) so if you were trying to get the info for the app during that time you would get nothing.

So my question is there a better, way of doing this than outputting an XML file (maybe JSON, I know that would be faster for getting the data to the phone but I'm not sure about if it is for making the file?) Or is there just a better way all together?

Thanks for the help in advance.

I think it is best to generate the data at the moment the request is made to your server. You can either extend your python script with socket functionality to listen on a certain port and then generate the data or have another service running which will call the python script and sends back it's result.

I prefer JSON over XML since it's in my opinion a cleaner syntax and easier to parse in the Android and iOS application.

--EDIT--

To create a python web service, you can use the web framework. I have to admit I have very little experience with python, but I believe this tutorial will point you in the right direction.

To call your python script from for example a php file, you could do something like this:

$storeTimes = system('python storescript.py myargs', $retval); 

You could then place the php file in your web directory and access it via HTTP. Make sure you output your data in JSON.

Note that above to options are by far not a complete RESTfull webservice. I do not know details about the scale of your application, but if you want to learn more you could read this explanation

There are a few things you can do. I think the best idea would be:

A)

Write the script in javascript (your python script) and then just execute them on the clients. This would enable you to just have one codebase and avoid calling your web service all of the time (+ you would have just 1 static file!)

B)

Do what @TMKVU suggested

C)

Run your cron every 5 or 30minutes and just include the time when it was actually run. Then you could just subtract the time:

Say it was generated 10minutes ago, then you would just subtract 10minutes from the 'closing in' statement.

ANYWAY:

If you can avoid it throw away the 'human readable' part 'closing in ...' from you XML or JSON and do that on the devices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM