python - 使用 Python 在新线程中创建 csv 文件
问题描述
我想使用新线程创建一个 csv 文件。下面是我写的代码:
import GPS_lib as GPS
import time
from threading import Thread
import csv
def GPSThread():
try:
GPS.gps=GPS.InitConnection()
last_print_gps=time.time()
with open(GPS.PATH,"w") as csvfile:
fieldnames=['Time','Latitude','Longitude','Speed','CoG']
writer=csv.DictWriter(csvfile,fieldnames=fieldnames)
writer.writeheader()
while True:
GPS.gps.update()
current_gps=time.time()
print(GPS.LATITUDE)
if current_gps-last_print_gps>=1:
last_print_gps=current_gps
GPS.GetGPSValues()
writer.writerow({
'Time':GPS.TIME,
'Latitude':GPS.LATITUDE,
'Longitude': GPS.LONGITUDE,
'Speed': GPS.SPEED,
'CoG': GPS.CoG})
except:
print("Error")
GPS_thread=Thread(target=GPSThread,args=())
GPS_thread.start()
GPS_thread.join()
如果我在没有新线程的情况下使用此代码,则一切正常。使用新线程我只有空文件。为什么?有人能帮我吗?
解决方案
推荐阅读
- powershell - 如何比较这些对象?
- excel - 基于另一个范围值的单元格值范围
- python - 如何查找一个字典键是否存在于其他字典值中。并从第一个字典中删除匹配值
- kotlin - IntelliJ 中没有运行/调试 TornadoFX 应用程序的选项
- shopify - Shopify 主题 - 主题样式设置不在 settings_schema.json 文件中?
- javascript - ReferenceError:JSEncrypt 3.0.0 之后未定义 JSENCRYPT_VERSION
- mongodb - Mongo DB 过滤其他集合而不从外部集合中投影任何字段
- kubernetes - MariaDB 服务器与 MariaDB Galera 集群 HA 复制
- sql - 如何在 Postgres 中使用 CTE 提高 GROUP BY 性能
- python - 来自两个或多个数字的 n 长度组合,具有重复限制