多处理?穿线?无限循环?我该怎么办?

时间:2018-06-27 23:16:07

标签: python multithreading loops while-loop multiprocessing

我正在尝试了解解决以下问题的最有效方法。我有来自多个来源(使用pyserial)的数据中的python代码池。我的目标是生成一个熊猫表,其中包含我收集的所有这些数据(从三个来源)。所有数据必须同时收集,因此我将不得不使用线程或多处理,并且能够使所有程序同时启动和停止。这是我的代码的总体思路:

User_inputs
###########
functions
###########
Port/Serial Misc.
###########
loops (can be defined in functions) e.g:

def lightloop():
   data = { 'light': []}
   df = pd.DataFrame(data, columns=[ 'light'])
   while True:
       arduino.reset_input_buffer()
       value = float(str(arduino.readline()))#everysecond
       df = df.append({'light': value}, ignore_index=True)
       print df
       time.sleep(1)
       if len(df.index) > 5:
           break  
   return df

###########
code that will analyze the data collected in the loops e.g

datatime = {'time': []}        
df2 = pd.DataFrame(datatime, columns=[ 'time'])
for i in range(len(df['light'])):      
    df2 = df2.append({'time': i}, ignore_index=True)
    df['time'] = df2['time']

print df
df.to_csv('~/Downloads/test.csv', sep='\t')
plt.scatter(df['time'], df['light'])
afont = {'fontname':'Avenir Book'}
mfont = {'fontname':'Menlo Regular'}
plt.title('',**mfont)
plt.ylabel('light', **afont)
plt.xlabel('Time [Second]', **afont)
plt.style.use('ggplot')
plt.savefig(os.path.expanduser('~/Downloads/test.png'), dpi = 150)

解决这个问题的最佳方法是什么?我已经研究了线程,它不是很简单,并且使我的代码变得非常复杂,我也无法停止线程以运行我的数据分析代码。

0 个答案:

没有答案
相关问题