线程帮助可能吗?

时间:2016-02-09 18:35:15

标签: python multithreading matplotlib

所以我需要让这两件事同时运行

    savedFile = open('Exchange_Rates' + time.strftime("_%d.%m.%Y.log"), 'w')
while spot < 60:
    savedFile.write(str(currencies.get_rate()) + ',' + str(spot) + '\n')
    savedFile.flush()
    spot += 1
    time.sleep(1)
savedFile.close()

和这个

def animate(i):
    pullData = open('Exchangerates' + time.strftime('_%d.%m.%Y.log'), 'r').read()
    dataArray = pullData.split('\n')
    xar = []
    yar = []
    for eachLine in dataArray:
        if len(eachLine) > 1:
            x, y = eachLine.split(',')
            xar.append(int(x))
            yar.append(int(y))
     ax1.clear()
     ax1.plot(xar, yar)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()

我是否需要使用线程模块或我将如何去做。或者我是否使用其他东西,如果是这样的话?

1 个答案:

答案 0 :(得分:0)

是的,我认为您需要线程模块。我会建议你阅读这篇文章http://softwareramblings.com/2008/06/running-functions-as-threads-in-python.html

最简单的方法:

 import thread

 def someFunc():
    print "someFunc was called"

thread.start_new_thread(someFunc, ())

更先进的方式:

import threading

t1 = threading.Thread(target=someFunc)
t1.start()
t1.join()