php - python how to write data into a single file with different call -
i have python script , php page on website. have create csv file , write data onto in following manner-
php processes get
requests , calls python script shell_exec
, passing get
variables it, json format. script here looks like-
... $var_a = $_get['a']; $var_b = $_get['b']; $var_c = $_get['c']; $post_data = array('item' => 'get-list', 'var a' => "$var_a", 'var b' => "$var_b", 'var c' => "$var_c" ); $json_data = json_encode($post_data); $temp = shell_exec('python process_data.py "' . $json_data .'"'); echo $temp; ...
this script quite straight forward, collects get
variables , forms json
object of it. data passed process_data.py
, processes data , saves data csv file, code is-
import sys, json, time, csv json_data = json.dumps(sys.argv[1]) def scr(json_data): json_data = json_data count = 0 json_list = json_data.split(',') in json_list: count=count+1 if(count==5): lati = json_list[0].strip('{') longi = json_list[1] datestmp = json_list[2] timestmp = json_list[3] out = open('file.csv', 'w') out.write('%s;' % lati.split(':')[1]) out.write('\n') out.write('%s;' % longi.split(':')[1]) out.write('\n') out.write('%s;' % datestmp.split(':')[1]) out.write('\n') out.write('%s;' % timestmp.split(':',1)[1]) out.write('\n') out.close() print 'latitude: ',lati.split(':')[1],', longitude: ',longi.split(':')[1],', datestamp: ',datestmp.split(':')[1],', time: ', timestmp.split(':',1)[1] else: print 'wrong data received' scr(json_data)
all things working well. data processed , saved. php script saves current data database. php 2 operations- 1. saves data database 2. pass data python script saves data csv file.
however, get
data coming @ regular time-interval(say 1 or 2 seconds). so, call python file gets regular these intervals. have open csv file every 1-2 seconds, write data there. slow down processing.
i want csv file remain open till data coming, , close when get
request not made long time. there way so?
Comments
Post a Comment