import asyncio
import time
import csv
import queue
import aiosqlite
import time
conn = None
data_queue = queue.Queue()
async def get_connection():
global conn
if conn is None:
conn = await aiosqlite.connect('lac_ci.db', check_same_thread=False)
return conn
async def query(row):
lac = row[0]
ci = row[1]
conn = await get_connection()
cursor = await conn.execute("SELECT lon,lat,radius FROM LacCiT WHERE lac = ? AND ci = ?",(lac, ci))
result = await cursor.fetchall()
if(len(result)!=0):
result = result[0]
lat = result[0]
lon = result[1]
radius = result[2]
row.extend([str(lon),str(lat),str(radius)])
data_queue.put(row)
async def write_csv(filename):
with open(filename, 'w', newline='') as file:
writer = csv.writer(file)
while True:
try:
row = data_queue.get(timeout=1)
writer.writerow(row)
data_queue.task_done()
except queue.Empty:
print("队列为空,说明数据已经全部处理完毕")
break
async def main():
start_time = time.time()
print(f"started at {time.strftime('%X')}")
csv_file = 'lac_tt.csv'
output_file = 'new_file.csv'
file = open(csv_file, 'r', newline='')
reader = csv.reader(file)
tasks = []
for row in reader:
task = asyncio.create_task(query(row))
tasks.append(task)
await asyncio.gather(*tasks)
await write_csv(output_file)
print(f"finished at {time.strftime('%X')}")
end_time = time.time()
elapsed_time = end_time - start_time
print(f"程序运行时间:{elapsed_time:.2f} 秒")
await conn.close()
coro = main()
asyncio.run(coro)

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95