I am trying to write a simple webserver to serve as an interface for an external hardware card I have purchased. The sample code provided by the manufacturers of the hardware card initializes the card like this:
def startCard(self):
while True:
if self.FeedbackOn:
if self.in_q.empty():
print("no new updata")
self.vCalcNewData (self.pnData, self.lSetChannels.value, self.qwSamplePos, self.lNumAvailSamples)
else:
print("update now")
new_par = self.in_q.get()
x_data = new_par["x_data"]
y_data = new_par["y_data"]
combined_data = [x_data]+[y_data]
self.newdata = np.column_stack(combined_data).flatten() #Here we have the new data for the new signal
self.loadData(self.newdata) #load the data to precalculated_data
self.precalSignal()
self.vCalcNewData(self.pvBuffer, self.lSetChannels.value, self.qwSamplePos, self.lNumAvailSamples)
else:
self.vCalcNewData(self.pnData, self.lSetChannels.value, self.qwSamplePos, self.lNumAvailSamples)
spcm_dwSetParam_i32 (self.hCard, SPC_DATA_AVAIL_CARD_LEN, self.lNotifySize_bytes)
self.qwSamplePos += self.lNumAvailSamples
As far as I understand this, it is listening for updates to a Queue object ("in_q") and runs in a while loop.
I am trying to wrap this in a Flask server and was trying to use the multiprocessing library to run the webserver and this while loop concurrently, as per this other related StackOverflow post:
from flask import Blueprint, render_template, request, redirect, url_for, send_file
import os, json, time
from queue import Queue
import multiprocessing as mp
from .awg import awg_interface, waveform_gen, camera_shot
# Blueprint Configuration
main_bp = Blueprint('main_bp', __name__)
q = Queue()
p = Process(target=awg_interface.Static2D_mode, args=(q,))
p.start()
p.join()
@main_bp.route('/submit', methods=['POST'])
def submit():
data = request.json['data']
q.put(data)
image = camera_shot.camera_shot()
return image
However, it seems like you cannot pass a Queue object as an argument to a multiprocessing Process; I get the error: TypeError: cannot pickle '_thread.lock' object.
Is there a simple way to run these two processes (the card and the webserver) simultaneously?
Thanks!