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()

process_thread_action的更多相关文章

随机推荐

  1. 成长笔记--解决Eclipse 变量名的自动补全问题

    大家使用eclipse敲代码的时候,是不是都被这样一个问题困扰着.就是键入一个变量名的时候,会自动提示补全:在你的变量名后面加上类型的名字!这个时候,你就必须键入Esc才不会自动补全你的变量,如果你键 ...

  2. Mac环境下svn命令行的使用

    在Windows环境中,我们一般使用TortoiseSVN来搭建svn环境.在Mac环境下,由于Mac自带了svn的服务器端和客户端功能,所以我们可以在不装任何第三方软件的前提下使用svn功能,不过还 ...

  3. JAVA实现EXCEL公式专题(四)——字符串函数

    直接上代码: /** * 项目名称: * 文件说明: ExCEL公式类型:字符串公式 * 主要特点: * 版本:1.0 * 制作人:刘晨曦 * 创建时间:2013-12-3 **/ package E ...

  4. 怎样mac上安装apk到连接数据线的手机

    高大上的mac俺也用了一段时间了.不知道大家有木有同一个烦恼.曾经在win上的时候仅仅要安装了应用宝之类的手机助手.就能够双击APK,直接安装到连接数据线的手机上,非常方便哈,可是mac上不行.近期找 ...

  5. jmeter 压测工具

    Apache jmeter 压力测试 java 环境安装 https://www.cnblogs.com/smyhvae/p/3788534.html 下载地址和文档 http://itopic.or ...

  6. 2016年蓝桥杯C/C++B组

    第一次參加蓝桥杯.也是有非常多感触的,时间全然不够写最后一题... 最后一题没做...还有全排序非常重要... 1. 煤球数目 有一堆煤球,堆成三角棱锥形.详细: 第一层放1个, 第二层3个(排列成三 ...

  7. Java String 常用函数

    1>获取  1.1:字符串中包含的字符数,也就是字符串的长度.   int length():获取长度  1.2:根据位置获取位置上某个字符.   char charAt(int index) ...

  8. Struts2学习九----------处理结果类型(input)

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2处理结果类型 - SUCCESS:Action正确的执行完成,返回相应的视图,success是name属性的默认值 - ERROR:表示 ...

  9. 非spring托管的类使用spring脱管的类。

    import org.springframework.beans.BeansException; import org.springframework.context.ApplicationConte ...

  10. 【puppeteer+Node.js】学习

    总结了一下有关puppeteer的学习的网站,以后还会继续更新 puppeteer 介绍 Puppeteer是一个通过DevTools Protocol控制headless chromium的高级no ...