Writing to a remote MySql DB with Python
I'm new to coding so I do apologise for any blatant faux pas I may made in my design concept.
I am looking to design a very basic app using Python which will scan for Access Points and log their details in a remote MySql database. My code works fine at present and writes the information to a locally held database no problem but now my query is, what pitfalls will their be when looking to insert the values into a remote DB? The broadband connection will be a 3G mobile connection and my concern is, if ran in a blackspot with no 3G coverage, how can I stall it so that the data is just held locally until coverage re开发者_JS百科turns?
Here's my code thusfar:-
while 1:
s.send('\n!0 enable CLIENT bssid,mac,manuf,type,minlat,minlon,maxlat,maxlon,agglat,agglon,bestlat,bestlon,gpsfixed,signal_dbm,noise_dbm,firsttime,lasttime')
buff = s.recv(512)
tmp = buff.split()
bssid = tmp[1]
mac = tmp[2]
manuf = tmp[3]
type = tmp[4]
minlat = tmp[5]
minlon = tmp[6]
maxlat = tmp[7]
maxlon = tmp[8]
agglat = tmp[9]
agglon = tmp[10]
bestlat = tmp[11]
bestlong = tmp[12]
gpsfixed = tmp[13]
signal_dbm = tmp[14]
noise_dbm = tmp[15]
firsttime = tmp[16]
lasttime = tmp[17]
if bssid != mac:
cursor.execute("""INSERT INTO data (bssid, mac, manuf, type,
minlat, minlon, maxlat, maxlon, agglat, agglon, bestlat, bestlong,
gpsfixed, signal_dbm, noise_dbm, firstime, lasttime)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s)""",
(bssid, mac, manuf, type, minlat, minlon,
maxlat, maxlon, agglat, agglon, bestlat, bestlong, gpsfixed,
signal_dbm, noise_dbm, firstime, lasttime) )
else:
pass
As said, the code works fine and so it's not really a question about my code but more about what I should consider when using a remote MySql DB and mobile broadband coverage. As I will be scanning access points whilst out and about, I am aware that I could have no 3G coverage for 10 minutes for example and will have detected, say 200 access points so just wondering how is best to deal with this? Write them to RAM, a temp file or a local DB and then update when 3G connection is back in place?
Thanks
You could launch a separate thread (using the threading module) to handle writes to the database. Then just pass the information to be written to that thread (using the Queue module, perhaps) and carry on.
The database thread can try the remote write and if it fails then write the data to a temporary file on the disk, sleep for 30 seconds and try it again.
精彩评论