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 加载(和功能)正常,无限循环似乎也可以正常工作,但它似乎无法 .pop() 来自“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 list.

产生一个索引错误,表明列表是空的,即使 gui 加载数据并将数据附加到列表中也是如此。

任何帮助将不胜感激,我一直在努力解决这个问题几个小时:(

感谢您的时间!

4

1 回答 1

2

每个进程都有它自己的列表版本。Python 列表不是进行进程间通信的合适方式。你应该看看使用multiprocessing.Queue

在使用多处理时使用“main”作为函数或模块名称也可能很危险。多处理库在内部使用此名称,您可能会遇到问题。

于 2013-04-12T18:24:19.270 回答