如何在不写入文本文件作为进程间通信的情况下让两个进程进行通信?
我的问题是"当运行多个python进程时,为什么第二个进程在kivy退出之前不会运行?"
This part of the question has been answered by
@MikhailGerasimov
在主持人删除此问题之前,请允许我给您一些背景知识。
感谢您审核我的问题: - )
from multiprocessing import Process
import timeit
import glob
import matplotlib.image as mpimg
import imutils
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.clock import Clock
from kivy.event import EventDispatcher
flag_path = 'status.dat'
Builder.load_string('''
<CameraClick>:
orientation: "vertical"
# STATUS BAR
BoxLayout:
orientation: "vertical"
Button:
id: statusBar
text: root.nn_flag
font_size: 40
text_size: self.size
halign: 'center'
valign: 'middle'
# EXIT BUTTON
BoxLayout:
orientation: "vertical"
Button:
id: btnExit
text: "Exit"
font_size: 40
on_press: app.stop()
''')
class CameraClick(BoxLayout):
nn_flag = StringProperty()
def __init__(self, **kwargs):
super(CameraClick, self).__init__(**kwargs)
self.nn_flag = 'Starting'
def callback(self, dt):
# Get Status of the algorithm
try:
with open(flag_path) as f:
self.nn_flag = f.readlines()[0].strip()
print('\n\nSTATUS:',self.nn_flag)
except Exception:
self.nn_flag = 'Empty...'
def start(self):
# Schedule File Polling
Clock.schedule_interval(self.callback, 0.5)
class TestCamera(App):
def build(self):
widget = CameraClick()
widget.start()
return widget
def saveToFile(mylist, filename='items.dat'):
with open(filename, 'w+') as myfile:
for item in mylist:
myfile.write("%s\n" % item)
def runInParallel(*fns):
proc = []
for fn in fns:
p = Process(target=fn)
p.start()
proc.append(p)
for p in proc:
p.join()
def doSomeWork():
print('Starting NN....')
# Load Images
images = glob.glob('*.JPG')
# Write to file
saveToFile(['Applying function'], filename=flag_path)
for idx, fname in enumerate(images):
print('doSomeWork_'+fname)
image = mpimg.imread(fname)
image = imutils.resize(image, height=300)
image = imutils.rotate_bound(image, 180)
# Write to file
saveToFile(['Done!'], filename=flag_path)
## MAIN
if __name__ == '__main__':
# Start timer
tic = timeit.default_timer()
# Start Processes
runInParallel( TestCamera().run(), doSomeWork)
toc = timeit.default_timer()
elapsed = toc-tic
print('Elapsed time (sec)', elapsed)