Skip to content
Snippets Groups Projects
Commit 2522d6b9 authored by Michael DM Dryden's avatar Michael DM Dryden
Browse files

First multiprocessing test. Works for CV.

parent ab989815
Branches
Tags
No related merge requests found
#!/usr/bin/env python
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
pool = Pool(processes=4) # start 4 worker processes
result = pool.apply_async(f, [10]) # evaluate "f(10)" asynchronously
print result.get(timeout=1) # prints "100" unless your computer is *very* slow
print pool.map(f, range(10)) # prints "[0, 1, 4,..., 81]"
\ No newline at end of file
......@@ -8,6 +8,7 @@
/* Begin PBXFileReference section */
5F87883C19072E86007B53E0 /* mpltest.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = mpltest.py; sourceTree = "<group>"; };
5FB75EE819268F15006346E1 /* dstat.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = dstat.py; sourceTree = "<group>"; };
5FCB541B190591CD00CEB148 /* interface */ = {isa = PBXFileReference; lastKnownFileType = folder; path = interface; sourceTree = "<group>"; };
5FCB541D1905923800CEB148 /* interface_test.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = interface_test.py; sourceTree = "<group>"; };
5FCB54231905B6EE00CEB148 /* dstat_comm.py */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.python; path = dstat_comm.py; sourceTree = "<group>"; };
......@@ -19,6 +20,7 @@
5FDC0DF218FDACDA003F857A = {
isa = PBXGroup;
children = (
5FB75EE819268F15006346E1 /* dstat.py */,
5F87883C19072E86007B53E0 /* mpltest.py */,
5FCB541D1905923800CEB148 /* interface_test.py */,
5FCB54231905B6EE00CEB148 /* dstat_comm.py */,
......
#!/usr/bin/env python
import serial, io, time, struct
import serial, io, time, struct, sys, os
from types import *
from serial.tools import list_ports
import numpy as np
import multiprocessing as mp
from Queue import Empty
class delayedSerial(serial.Serial): #overrides normal serial write so that characters are output individually with a slight delay
def write(self, data):
......@@ -33,6 +35,44 @@ class linearData:
self.ax = []
self.ay = []
class dataCapture(mp.Process):
def __init__(self, ser_instance, pipe):
mp.Process.__init__(self)
self.serial = ser_instance
self.recv_p, self.send_p = pipe
# self.data_queue = mp.Queue()
def run(self):
sys.stdout.write('[%s] running ... process id: %s\n'
% (self.name, os.getpid()))
# self.recv_p.close()
while True:
for line in self.serial:
if line.startswith('B'):
voltage, current = struct.unpack('<Hl', self.serial.read(size=6)) #uint16 + int32
self.send_p.send([voltage, current])
elif line.lstrip().startswith("no"):
self.serial.flushInput()
self.send_p.close()
print "closed"
break
break
class dataUpdate(mp.Process):
def __init__(self, ser_instance):
mp.Process.__init__(self)
def run(self):
sys.stdout.write('[%s] running ... process id: %s\n'
% (self.name, os.getpid()))
class SerialDevices:
def __init__(self):
......@@ -99,8 +139,33 @@ class Experiment:
self.plot.redraw()
self.ser.close()
def data_handler(self):
recv_p, send_p = mp.Pipe(duplex=False)
capture_proc = dataCapture(self.ser, (recv_p, send_p))
capture_proc.start()
send_p.close() #pipe won't trip EOFError unless all connections are closed
#capture_proc.join()
updatetime = 0
while True:
try:
data = recv_p.recv()
self.data[0].append((data[0]-32768)*3000./65536)
self.data[1].append(data[1]*(1.5/self.gain/8388607))
if ((time.time() - updatetime) > .2):
self.plot.updateline(self, 0)
self.plot.redraw()
updatetime = float(time.time())
except EOFError:
print "empty"
break
"""def data_handler(self):
while True:
for line in self.ser:
if line.startswith('B'):
......@@ -124,7 +189,7 @@ class Experiment:
self.ser.flushInput()
break
break
break"""
def data_postprocessing(self):
pass
......
......@@ -8,10 +8,17 @@ except:
pass
try:
import gtk
import gobject
except:
print('GTK not available')
sys.exit(1)
try:
import gobject
except:
print('gobject not available')
sys.exit(1)
import interface.adc_pot as adc_pot
import interface.chronoamp as chronoamp
import interface.lsv as lsv
......@@ -22,6 +29,7 @@ import interface.pd as pd
import interface.save as save
import dstat_comm as comm
from serial import SerialException
import multiprocessing
import mpltest
......@@ -304,5 +312,7 @@ class main:
if __name__ == "__main__":
multiprocessing.freeze_support()
gobject.threads_init()
main = main()
gtk.main()
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment