如何将连续脚本的结果作为另一个可调用对象

时间:2015-05-07 00:19:40

标签: python

我正在尝试运行一个脚本,该脚本将不断从GPS模块获取新数据并将其存储在队列中,以便另一个脚本调用它。

我将此脚本用作gps_data.py:

running = None
gpsd = None

os.system('clear') #clear the terminal (optional)

class GpsPoller(threading.Thread):

    QUEUE_MAX_SIZE = 50

    def __init__(self):
        threading.Thread.__init__(self)
        global gpsd
        gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
        self.current_value = None
        global running
        running = True #setting the thread running to true
        self.queue = Queue.Queue(maxsize=self.QUEUE_MAX_SIZE)

    def run(self):
        while running:
            gpsinfo = gpsd.next() #this will continue to loop and grab EACH set$
            # Here I'm just populating the queue with the return of gpsd.next()$
            # you can choose what you specifically want to populate it with.
            # like gpsinfo = [gpsd.fix.latitude, gpsd.fix.longitude]
            self.queue.put_nowait(gpsinfo) #add the gpsinfo to the queue for ot$

我正在尝试将该数据导入another_script.py

这是我的尝试:

文件夹结构:

manage.py
hud/
    another_script.py
    gps_data.py

another_script.py

from gps_data import GpsPoller
import Queue

g = GpsPoller()
g.start()
run = True
while run:
    try:
        # Get item for the GpsPoller queue
        gpsinfo = g.queue.get(True, 1) # blocking with a timeout of 1 sec
        print 'gpsinfo:', gpsinfo
        g.task_done() # tell the queue that the task is done (you are done usin$
    except Queue.Empty:
        print 'Queue is Empty'
        run = False

我收到错误AttributeError: 'GpsPoller' object has no attribute 'task_done'

1 个答案:

答案 0 :(得分:1)

我会使用Queue。您可以让GpsPoller在其自己的线程中运行,填充队列。在您的another_script.py线程中,您可以读取该队列,如果该队列中有项目,则打印该项目。

由于我没有gps脚本/ hw我无法为你测试代码,但它会与此类似:

在你的gps_data.py中:

import os
from gps import *
from time import *
import time
import threading
import Queue

gpsd = None #seting the global variable

os.system('clear') #clear the terminal (optional)

class GpsPoller(threading.Thread):

    QUEUE_MAX_SIZE = 50

    def __init__(self):
        threading.Thread.__init__(self)
        global gpsd #bring it in scope
        gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
        self.current_value = None
        self.running = True #setting the thread running to true
        self.queue = Queue.Queue(maxsize=self.QUEUE_MAX_SIZE)

    def run(self):
        global gpsd
        while self.running:
            gpsinfo = gpsd.next() #this will continue to loop and grab EACH set of gpsd info to clear the buffer
            # Here I'm just populating the queue with the return of gpsd.next(), 
            # you can choose what you specifically want to populate it with.
            # like gpsinfo = [gpsd.fix.latitude, gpsd.fix.longitude]
            self.queue.put_nowait(gpsinfo) #add the gpsinfo to the queue for others to read

而other_script.py看起来像这样:

import gps_data.GpsPoller
import Queue

g = GpsPoller()
g.start()
run = True
while run:
    try:
        # Get item for the GpsPoller queue
        gpsinfo = g.queue.get(True, 1) # blocking with a timeout of 1 sec
        print 'gpsinfo:', gpsinfo
        g.queue.task_done() # tell the queue that the task is done (you are done using gpsinfo)
    except Queue.Empty:
        print 'Queue is Empty'
        run = False