wxPython非阻塞GUI线程和多处理? [英] wxPython non-blocking GUI threading AND multiprocessing?

查看:234
本文介绍了wxPython非阻塞GUI线程和多处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 2.7.3 x64 wxPython 2.8 x64

Python 2.7.3 x64 wxPython 2.8 x64

在阅读有关python线程和多处理的文章时,尤其是Doug Hellmann撰写的一些文章对本书有很大帮助.但是,我对一件事感到困惑...

Been reading quite a bit on python threading and multiprocessing, particularly some articles by Doug Hellmann, which have helped tremendously. However, I'm confused about one thing...

认为 Python多处理模块或多或少是线程模块的直接替代品,除了args必须是可腌制的,但我发现这是为了不阻塞在我的GUI中,我必须首先使用threading.Thread创建一个新线程,然后在该线程中使用multiprocessing.Process进行多进程.这行得通,而且行之有效,但是对我来说似乎有点不合时宜.

I thought the Python multiprocessing module was more-or-less a drop-in replacement for the threading module, excepting that args must be picklable, but I'm finding that in order not to block my GUI, I must first create a new thread with threading.Thread then multiprocess within that thread with multiprocessing.Process. This works, and works well, but it seems a bit kludgey to me.

如果我尝试不先执行线程就直接进行多进程处理,则我的GUI仍将阻塞,直到完成多处理作业为止.是按设计工作的,还是我缺少有关多处理模块的基本知识?

If I try to directly multiprocess without first threading, then my GUI still blocks until the multiprocessing job is done. Is that working as designed, or am I missing something fundamental about the multiprocessing module?

如果需要示例,我可以提供.

If examples are needed, I can provide them.

谢谢

-RMWChaos

-RMWChaos

要求提供示例...

假设onProcess()由GUI中的按钮触发,这将阻止GUI ...

Assuming that onProcess() is triggered by a button in the GUI, this blocks the GUI...

import time
import multiprocessing as mp

def myWorker(a, b):
    time.sleep(0.1)
    print '{} * {} = {}'.format(a, b, a*b)

def onProcess(event):
    jobs = mp.cpu_count() * 2
    a = 5
    b = 10

    for job in range(jobs):
        mp.Process(target = myWorker, args = (a, b,)).start()

这不是...

import time
import multiprocessing as mp
import threading as th

def myWorker(a, b):
    time.sleep(0.1)
    print '{} * {} = {}'.format(a, b, a*b)

def onProcess(event):
    a = 5
    b = 10
    th.Thread(target = myThread, args = [a, b,]).start()

def myThread(a, b):
    jobs = mp.cpu_count() * 2

    for job in range(jobs):
        mp.Process(target = myWorker, args = (a, b,)).start()

推荐答案

多线程和多处理本质上是不同的.

Multi-threading and multi-processing are fundamentally different.

大多数情况下,线程用于I/O.当您创建新线程时,它与从中生成线程的程序所包含的进程相同.这意味着它与程序共享内存空间,但是它们(程序和线程)不能并行运行(也要查找GIL).

Threads are used for i/o for the most part. When you make a new thread it is contained within the same process as the program you spawned the thread from. What that means is that it shares memory space with the program, but they (the program and the thread) cannot run in parallel (also look up GIL).

另一方面,多进程在OS级别上产生了一个新进程.此新进程可以与先前存在的进程并行运行,但是不会与产生该进程的程序共享内存空间.当您要加速的代码与I/O不相关,而与实际的处理器密集型计算有关时,此功能将更为有用.

Multi-processing on the other hand spawns a new process on the OS level. This new process can run in parallel with the pre-existing process, but it does not share memory space with the program that spawned it. This is more useful when the code you want to speed up is not i/o related but actual processor intensive computation.

对于gui,您通常希望对gui的不同部分使用线程,以便在gui的一部分中运行某些操作不会锁定整个gui,直到处理结束.

For a gui, you mostly want to use threading for different parts of the gui so that running something in one part of the gui does not lock up your entire gui until that handling ends.

没有代码很难说,但是我相信您不应该特别需要在新线程中生成一个进程,而只需让该线程来处理该进程即可.但是,如果该线程需要处理大量的计算类型的处理而不是大量的I/O,那么您将需要启动一个进程来处理它.

Without code it is hard to tell, but I believe you should not particularly need to spawn a process within the new thread, but instead just have that thread handle the processing. However, if that thread needs to handle intensive computational type of processing as opposed to lots of i/o, then you would want to start a process to handle that.

我相信您的大部分问题都在于对多处理与多线程的误解.

I believe that most of your problem lies in misunderstanding multi-processing vs multithreading.

这篇关于wxPython非阻塞GUI线程和多处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆