python - 如何使用线程获取数据并将数据写入同一个文本文件

标签 python multithreading python-2.7 python-3.x python-multithreading

关于这些与以下代码相关的数据,

12      41      58      82000
12      41      58      190000
12      41      58      301000
12      41      58      416000
12      41      58      524000
12      41      58      632000
12      41      58      741000
12      41      58      849000
12      41      58      959000
12      41      59      65000
12      41      59      174000
12      41      59      281000
12      41      59      389000
12      41      59      496000
12      41      59      605000
12      41      59      711000
12      41      59      820000
12      41      59      927000
12      42      0      36000
12      42      0      143000
12      42      0      252000
12      42      0      360000
12      42      0      469000
12      42      0      577000
12      42      0      685000
12      42      0      793000
12      42      0      901000
12      42      1      9000

代码在这里:

from datetime import datetime
minute = 0
second = 0
microsecond=0
fob=open('test_file_thread.txt','w')

def st():
    global hour
    global minute
    exacttime = datetime.now()
    hour = exacttime.hour
    minute = exacttime.minute

def nd():
    global second
    global microsecond
    exacttime = datetime.now()
    second = exacttime.second
    microsecond = exacttime.microsecond

while True:
    st()
    nd()
    fob.write(str(hour) + '      '
              + str(minute) + '      '
              + str(second) + '      '
              + str(microsecond) + '\n')
    time.sleep(0.1)

现在我想对线程做同样的事情。 我正在使用两个线程从两个串行端口获取数据。由于线程并行工作,我无法制作一个文本文件来逐行排列数据。也就是说,我想从第一个串口获取数据,将其写入文本文件的row=0和column=0,然后从第二个串行端口读取数据并将其写入行=0和列=5(因此它正好位于第一个端口数据的前面),然后转到下一行(行=1)并重复此过程。 我想要类似我上面通过串行端口读取所做的“时间示例”。这是我的代码,我不知道如何修复它以及在哪里放置命令 fob.write(str(x)+' ' +str(y)+'\n') 来写入每件事都在同一时间到同一个文件。 感谢您的帮助

import time, threading
import threading
import serial
x=0
y=0
fob=open('test_file_thread.txt','w')
ser1 = serial.Serial("COM5", baudrate=9600, timeout=1)
ser2 = serial.Serial("COM4", baudrate=9600, timeout=0.25)

def st():
    global x
    while True:
        x = ser1.readline()
def nd():
    global y
    while True:
        y= ser2.readline()

threading.Timer(0, st).start()
threading.Timer(0, nd).start()
fob.write(str(x)+'      ' +str(y)+'\n')

最佳答案

你应该使用一个队列,实际上是两个!

from Queue import Queue
...
ST_Q = Queue()
MD_Q = Queue()

def st():
    ST_Q.put(ser1.readline())

def md():
    MD_Q.put(ser2.readline())

threading.Timer(0, st).start()
threading.Timer(0, nd).start()
#while loop here?    
fob.write(str(ST_Q.get())+'      ' +str(MD_Q.get())+'\n')

关于python - 如何使用线程获取数据并将数据写入同一个文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41075378/

相关文章:

python - 在线程操作期间避免 PyQt5 GUI 卡住?

python - pytest-django依赖注入(inject)

python - dbus-python 如何返回字典数组

python - 如何从 Fabric 的本地命令捕获 stderr?

python - 猴子修补、鸭子打字和论证 self

python - 文本分割 : Algorithm to match input with the longest words from the dictionary

python - 为什么 __bool__ 内置函数必须在 dask.delayed 对象上引发异常?

node.js - Nodejs集群只将任务分配给一个worker(任意)

java - 单核cpu java中的volatile

java - 如何为多线程应用程序创建测试环境