Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python多处理导致整个脚本循环_Python_Multithreading_Python Multithreading - Fatal编程技术网

Python多处理导致整个脚本循环

Python多处理导致整个脚本循环,python,multithreading,python-multithreading,Python,Multithreading,Python Multithreading,看起来线程之间的多处理交换速度更快,所以我开始进行交换,但我得到了一些意想不到的结果。它会导致我的整个脚本在线程之前没有循环的情况下循环多次 代码段示例: stuff_needs_done = true more_stuff_needs_done = true print "Doing stuff" def ThreadStuff(): while 1 == 1: #do stuff here def OtherThreadStuff(): while 1 == 1:

看起来线程之间的多处理交换速度更快,所以我开始进行交换,但我得到了一些意想不到的结果。它会导致我的整个脚本在线程之前没有循环的情况下循环多次

代码段示例:

stuff_needs_done = true
more_stuff_needs_done =  true
print "Doing stuff"

def ThreadStuff():
  while 1 == 1:
    #do stuff here

def OtherThreadStuff():
  while 1 == 1:
    #do other stuff here

if stuff_needs_done == true:
  Thread(target=ThreadStuff).start()

if more_stuff_needs_done == true:
  Thread(target=OtherThreadStuff).start()
stuff_needs_done = true
more_stuff_needs_done = true
print "Doing stuff"


def ThreadStuff():
  while 1 == 1:
    #do stuff here

def OtherThreadStuff():
  while 1 == 1:
    #do other stuff here

if stuff_needs_done == true:
  stuffproc1= Process(target=ThreadStuff).start()

if more_stuff_needs_done == true:
  stuffproc1= Process(target=OtherThreadStuff).start()
这正是我所期望的。线程开始运行,直到停止。但是当运行很多这样的程序时,开销会更高,所以我被告知,所以我尝试切换到多处理

代码段示例:

stuff_needs_done = true
more_stuff_needs_done =  true
print "Doing stuff"

def ThreadStuff():
  while 1 == 1:
    #do stuff here

def OtherThreadStuff():
  while 1 == 1:
    #do other stuff here

if stuff_needs_done == true:
  Thread(target=ThreadStuff).start()

if more_stuff_needs_done == true:
  Thread(target=OtherThreadStuff).start()
stuff_needs_done = true
more_stuff_needs_done = true
print "Doing stuff"


def ThreadStuff():
  while 1 == 1:
    #do stuff here

def OtherThreadStuff():
  while 1 == 1:
    #do other stuff here

if stuff_needs_done == true:
  stuffproc1= Process(target=ThreadStuff).start()

if more_stuff_needs_done == true:
  stuffproc1= Process(target=OtherThreadStuff).start()
但似乎整个过程都会启动几次,所以会出现Doing stuff输出,并运行几个线程

我可以加入一些.join,但是没有循环会导致打印输出再次运行,这意味着它无处等待

我希望这只是一个语法问题,但我很难找到整个脚本循环的原因。我非常感谢任何指向正确方向的指针。

这在以下章节中提到:

主模块的安全导入

确保新的Python解释器可以安全地导入主模块,而不会造成诸如 开始一个新的过程

例如,在运行以下模块的Windows下,会出现运行时错误:

from multiprocessing import Process

def foo():
    print 'hello'

p = Process(target=foo)
p.start()
相反,应该使用if uuu name uuuuu=='uuuu main uuuuuuuu':来保护程序的“入口点”,如下所示:

from multiprocessing import Process, freeze_support

def foo():
    print 'hello'

if __name__ == '__main__':
    freeze_support()
    p = Process(target=foo)
    p.start()
这允许新生成的Python解释器安全地导入模块,然后运行模块的foo函数


几乎每行都有语法错误。请在你的帖子中更正它们。