import psycopg2
import threading conn_fmac = psycopg2.connect(database='filter_useless_mac', user='user', password='password', host='192.168.168.168',
port='5432') def fetch_rows(f_l):
r = {}
with conn_fmac:
with conn_fmac.cursor() as curs:
for i in f_l:
# http://initd.org/psycopg/docs/faq.html
# The arguments in the execute() methods can only represent data to pass to the query: they cannot represent a table or field name:
curs.execute('SELECT detail_data FROM apiv2_single_mac_with_res WHERE mac= %s LIMIT 1 ', (i,)) # psycopg2.InternalError: current transaction is aborted, commands ignored until end of transaction block
try:
t = curs.fetchone()
if t is not None:
r[i] = {}
r[i] = t[0]
except Exception:
continue
return r def update_rows(id, new_val):
with conn_fmac:
with conn_fmac.cursor() as curs:
try:
curs.execute(
'UPDATE control_group_with_compute_res SET mac_with_res_position_lat_lon_unique_num=%s WHERE oid_timestamp=%s',
(new_val, id))
print(threading.get_ident(), 'OK')
except Exception:
print(Exception) class MyThread(threading.Thread):
def __init__(self, func, args, name):
threading.Thread.__init__(self)
self.name, self.func, self.args = name, func, args def run(self):
self.func(self.args) def main():
with conn_fmac:
with conn_fmac.cursor() as curs:
curs.execute(
'SELECT oid_timestamp,mac_with_res_position FROM control_group_with_compute_res WHERE mac_with_res_position IS NOT NULL ORDER BY oid_timestamp DESC ')
tuple_l = curs.fetchall() tn, tl, tstep = len(tuple_l), [], 200 def tf(ts):
print(ts)
te = ts + tstep
te = min(te, tn)
for i in tuple_l[ts:te]:
oid_timestamp, mac_with_res_position = i
n_l = []
for k in mac_with_res_position:
longitude, latitude = mac_with_res_position[k]['longitude'], mac_with_res_position[k]['latitude']
s = '%s%s' % (longitude, latitude)
if s not in n_l:
n_l.append(s)
n = len(n_l)
if n > 0:
update_rows(oid_timestamp, n) for i in range(0, tn, tstep):
if i >= tn:
break
thread_instance = MyThread(tf, (i), tf.__name__)
tl.append(thread_instance) for t in tl:
t.setDaemon = False
t.start()
for t in tl:
t.join() if __name__ == '__main__':
main()
import json
import psycopg2
import threading conn_fmac = psycopg2.connect(database='filter_useless_mac', user='postgres', password='postgres', host='192.168.8.8',
port='5432') def update_rows(id, new_val):
with conn_fmac:
with conn_fmac.cursor() as curs:
try:
curs.execute(
'UPDATE control_group_with_compute_res SET add_lat_lon_to_original_res=%s WHERE oid_timestamp=%s',
(new_val, id))
print(threading.get_ident(), 'OK')
except Exception:
print(Exception) class MyThread(threading.Thread):
def __init__(self, func, args, name):
threading.Thread.__init__(self)
self.name, self.func, self.args = name, func, args def run(self):
self.func(self.args) def main():
with conn_fmac:
with conn_fmac.cursor() as curs:
sql = "SELECT tmp.oid_timestamp, ja.latitude, ja.longitude FROM ( SELECT oid_timestamp, detail_data ->> 'area_code' AS area_code FROM control_group_with_compute_res) tmp LEFT JOIN jmtool_areacode_longitude_latitude ja ON tmp.area_code = ja.area_code WHERE ja.area_code IS NOT NULL ORDER BY oid_timestamp ASC;"
curs.execute(sql)
tuple_l = curs.fetchall() tn, tl, tstep = len(tuple_l), [], 200 def tf(ts):
print(ts)
te = ts + tstep
te = min(te, tn)
for i in tuple_l[ts:te]:
oid_timestamp, lat, lon = i
r = {}
r['from'], r['latitude'], r['longitude'] = 'jmtool_areacode', lat, lon
update_rows(oid_timestamp, json.dumps(r, ensure_ascii=False)) for i in range(0, tn, tstep):
if i >= tn:
break
thread_instance = MyThread(tf, (i), tf.__name__)
tl.append(thread_instance) for t in tl:
t.setDaemon = False
t.start()
for t in tl:
t.join() if __name__ == '__main__':
main()

最新文章

  1. WinServer远程部署系统(RDSystem)
  2. js 控制框架页面跳转 top.location.herf = "url"
  3. Global和Globals
  4. Podfile升级后的影响
  5. SAP系统更改小数点显示问题
  6. System.arraycopy
  7. 搭建mongodb集群(副本集+分片)
  8. Tools Function
  9. 漫水填充算法 - cvFloodFill() 实现
  10. debug,trace,release项目配置区别
  11. js中数组Array的一些常用方法总结
  12. Ectouch修改虚拟销售数量的方法
  13. jQuery下实现检测指定元素加载完毕
  14. Sublime3 中在行尾增加一个分号的方法
  15. python webdriver api-对启动的火狐浏览器添加配置
  16. 用UDP实现聊天功能
  17. 项目实战:负载均衡集群企业级应用实战—LVS详解
  18. nohup: failed to run command `java': No such file or directory
  19. Android 图表
  20. OTU rank curve(Rank Abundance 曲线)【基本概念】

热门文章

  1. ELK之logstash收集日志写入redis及读取redis
  2. Hdoj 5181 numbers
  3. 盘点支持Orchard的.NET 4.5虚拟主机(虚拟空间)
  4. 求用delphi编写的LRC校验位算法函数,急!!!
  5. Displaying Modal Window Messages in Oracle Forms Using Show_Alert
  6. Flutter开发记录part3
  7. JAVA Eclipse ActivityManager Warning Activity not started, its current task has been brought to the front怎么办
  8. 谈论程序中的token
  9. java查看工具jinfo-windows
  10. 【Python】matplotlib绘制折线图