标签: multiprocess

Python 多处理帮助条件退出

我正在用 Python 进行多处理,但我对这个主题没有任何运气。基本上我有一个运行起来很耗时的程序。我需要在 1 到 100 的范围内运行它,但是一旦满足我正在寻找的条件,我想中止所有进程。条件是返回值 == 90。

这是一个非多进程代码块。谁能给我一个例子,说明他们如何将其转换为多进程函数,一旦满足“90”条件,代码将退出所有进程?

def Addsomething(i):
    SumOfSomething = i + 1    
    return SumOfSomething

def RunMyProcess():
    for i in range(100):
        Something = Addsomething(i)
        print Something
    return

if __name__ == "__main__":
    RunMyProcess()
Run Code Online (Sandbox Code Playgroud)

编辑:

我在测试第三个版本时遇到了这个错误。知道是什么原因造成的吗?

Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 554, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 507, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Python27\lib\multiprocessing\pool.py", line 379, in _handle_results
    cache[job]._set(i, obj)
  File "C:\Python27\lib\multiprocessing\pool.py", line 527, in _set
    self._callback(self._value)
  File "N:\PV\_Proposals\2013\ESS - …
Run Code Online (Sandbox Code Playgroud)

python multiprocessing multiprocess python-multithreading

2
推荐指数
1
解决办法
3832
查看次数

使用fork和MPI进行多进程编程之间的区别

使用linux"fork"创建多进程程序和MPI库中可用的函数之间在性能或其他方面是否存在差异?或者,由于可以使用的功能,它在MPI中更容易实现吗?

fork mpi multiprocess

2
推荐指数
1
解决办法
960
查看次数

我得到一个左值错误

    main(){

     helloworld();
}
void helloworld(){
     cout<<"hellowowrld";
}
Run Code Online (Sandbox Code Playgroud)

它输出你好世界,它有什么问题?

c system-calls multiprocess

1
推荐指数
1
解决办法
53
查看次数

在 Python 类的方法中线程化两个函数

我想在Python中同时在类的方法中运行2个函数。我尝试使用threading模块但它不起作用。我的示例代码如下:

import os, sys
import threading
from threading import Thread

class Example():
    def __init__(self):
        self.method_1()

    def method_1(self):

        def run(self):
            threading.Thread(target = function_a(self)).start()
            threading.Thread(target = function_b(self)).start()

        def function_a(self):
            for i in range(10):
                print (1)

        def function_b(self):
            for i in range(10):
                print (2)

        run(self)


Example()
Run Code Online (Sandbox Code Playgroud)

如果执行上面的代码,它只会1先打印所有 s,然后打印所有2s。然而,我想要的是同时1打印。2因此,所需的输出应该是它们混合在一起。

threading模块有能力做到这一点吗?如果不能的话,什么模块可以做到这一点?如果有人知道如何解决,请告诉我。赞赏!

python multithreading class function multiprocess

1
推荐指数
1
解决办法
8905
查看次数

如何通过 Python Selenium 使用多进程池

我有一些代码需要从数百个网页中获取数据,我想通过为其运行多个 Selenium Chrome 浏览器实例来加快速度。例如我这里有这个代码:

from selenium import webdriver
from multiprocessing import Pool
from tkinter import *

#initiate browser
def browser():
    global driver
    driver = webdriver.Chrome(r"C:\Users\areed\Desktop\p\chromedriver.exe")
    return driver

#test link
def test():
    links = [link1.com, link2.com, link3.com, link4.com]
    browser()
    for l in links:
        driver.get(l)
        dostuff(driver)

#Scrape Data
def dostuff(driver):
    print('doing Stuff')

#multiprocess Function      
def multip():
    pool = Pool(processes=4)
    pool.map(test())

#tkinter Window
if __name__ == "__main__":  
    win = Tk()
    win.title("test")
    win.geometry('300x200')
    btn = Button(win, text="Tester", command=multip)
    btn.pack()
    win.mainloop()
Run Code Online (Sandbox Code Playgroud)

我怎样才能让这段代码运行多个 selenium chrome 浏览器?这段代码在不添加多进程的情况下工作得很好。有人可以向我解释一下如何解决这个问题吗?谢谢!

python selenium pool multiprocess

0
推荐指数
1
解决办法
5040
查看次数