运行多个python进程时为什么第二个进程在kivy退出之前不会运行?

时间:2017-10-20 01:29:44

标签: python multiprocessing kivy python-multiprocessing

第2部分

如何在不写入文本文件作为进程间通信的情况下让两个进程进行通信?

第1部分

我的问题是"当运行多个python进程时,为什么第二个进程在kivy退出之前不会运行?"

This part of the question has been answered by @MikhailGerasimov

在主持人删除此问题之前,请允许我给您一些背景知识。

背景:

  • 我已经构建了一个具有GUI和后台运算的算法的应用程序。
  • 我希望它们作为两个独立的进程运行,因为算法可能需要10秒并阻止gui。
  • 为了缓解这种情况,我在两个单独的python文件中运行GUI和算法,这些文件通过文本文件相互通信,这是不理想的。
  • 以下是我正在使用的代码的摘录。
  • 当运行多个进程时,为什么第二个进程在kivy退出之前不会运行?下面的代码有什么问题?

感谢您审核我的问题: - )

示例代码

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)

0 个答案:

没有答案