1

マルチプロセッシングを使用してキュー (リスト) のチェック中に無限ループするプログラムを作成しようとして問題が発生しています

私のコードの簡略化されたバージョンはここにあります(GUIが含まれています):

    #!/usr/bin/env python
    # coding: latin-1

    import multiprocessing
    import time
    import subprocess
    import collections
    import os
    import random
    import string
    import sys
    import mimetypes
    import re
    import pygtk
    import gtk
    import gobject

    # set globals
    queue1 = []

    def do_stuff():
        global queue1
        print("Do_stuff has been called") # this is displayed
        ftype=''
        try:
            print("Attempting to get work to do")  # this is displayed
            qe = queue1.pop()
            print("Getting file name")  # this is NOT displayed, even if the on_queueadd_clicked button appends data to the queue.
            filein, data = qe.split("::=::")
            processfile(filein)
        except IndexError:      
            print("Nothing in Queue")

        return 0

    def queuecheck():
        while True:
            do_stuff()
            #time.sleep(0.1) # 100ms
            time.sleep(3)
    def processfile(filein):
        print ("PROCESSING FILE")
        fileout,fileout2 = os.path.splitext(filein)
        f = "-i '"+filein+"' -o '"+fileout+".o.txt'"
        #output1 = os.system("e.exe "+f)
        print("file has been made")
    def main():
        print("Loading Main Window ")
        loadui()
    class loadui():
        def on_queueadd_clicked(self, widget):
            global filebutton
            global queue1
            # get file location from gtk window -> FileChooserButton (removed for simplicity)

            stringadd = 'file.txt'+'::=::'+'some other data'

            print("The string we are adding is: ") # this displays the correct output
            print(stringadd)
            queue1.append(stringadd)

        def on_window_delete_event(self, w1, widget):
            self.sub_process.terminate()
            sys.exit(0)
            return True
        def update_text(self):
            try:
                data = self.data_queue.get_nowait()
            except:
                pass
            else:
                print(data)
            return True

        def __init__(self):
            builder = gtk.Builder()
            builder.add_from_string('<?xml version="1.0" encoding="UTF-8"?><interface>  <requires lib="gtk+" version="2.24"/>  <!-- interface-naming-policy project-wide -->  <object class="GtkWindow" id="window1">    <property name="can_focus">False</property>    <child>      <object class="GtkButton" id="queueadd">        <property name="label" translatable="yes">Add to queue</property>        <property name="visible">True</property>        <property name="can_focus">True</property>        <property name="receives_default">True</property>        <property name="use_action_appearance">False</property>        <signal name="clicked" handler="on_queueadd_clicked" swapped="no"/>      </object>    </child>  </object></interface>')
            handlers = {
                "on_queueadd_clicked": self.on_queueadd_clicked

            }
            builder.connect_signals(handlers)
            main = builder.get_object("window1")
            main.set_title("test script")
            # init queue
            self.data_queue = multiprocessing.Queue()
            gobject.timeout_add(100, self.update_text) 
            self.sub_process = multiprocessing.Process(target=queuecheck)
            self.sub_process.start()
            #.... ... ... 
            main.show_all()
            gtk.main()



    main()

GUI は正常にロード (および機能) します。無限ループも正常に機能しているように見えますが、「queue1」からのリスト エントリを .pop() できないようです。

問題を次のように切り分けました。

        print("Do_stuff has been called") # this is displayed
        ftype=''
        try:
            print("Attempting to get work to do")  # this is displayed
            qe = queue1.pop()
            print("Getting file name")  # this is NOT displayed, even if the on_queueadd_clicked button appends data to the list.

GUI がデータをロードしてリストに追加しても、リストが空であることを示す Index エラーが生成されます。

何か助けをいただければ幸いです。私はこれを数時間修正するのに苦労しています:(

お時間をいただきありがとうございます!

4

1 に答える 1

2

各プロセスには、そのリストの独自のバージョンがあります。Python リストは、プロセス間通信を行う適切な方法ではありません。multiprocessing.Queueの使用を検討する必要があります。

マルチプロセッシングを使用する場合、関数またはモジュール名として「main」を使用することも危険です。マルチプロセッシング ライブラリはこの名前を内部で使用するため、問題が発生する可能性があります。

于 2013-04-12T18:24:19.270 に答える