python - 使用 pygtk 和多处理( python )时无法 .pop() 列出

标签 python python-2.7 multiprocessing pygtk

我在尝试制作一个在检查队列(列表)时使用多重处理无限循环的程序时遇到问题

我的代码的简化版本在这里(它包括 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 加载数据并将数据附加到列表中也是如此。

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

感谢您的宝贵时间!

最佳答案

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

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

关于python - 使用 pygtk 和多处理( python )时无法 .pop() 列出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15978186/

相关文章:

python - 在 Numpy 中有效地构建一个 'rolled' 行的矩阵

python - 使用 PRAW 获取 Reddit 用户评论导致 TypeError : 'SubListing' object is not callable error

python - 如何让 WTForms 从数据存储中读取?

python - 为每个进程划分一个for循环

python - 如何在Python中正确使用多进程

python - 检测文本之间的空格(OpenCV、Python)

python - 预期为 ")"皮兰斯

python - 检查元素单击的 if 语句不会被拦截(Python Selenium)

python - 编辑脚本以说明两个列表的每个组合

Python套接字窃取对方的数据包