jjzjj

windows - `ProcessPoolExecutor` 在 Ubuntu 上工作,但在 Windows 10 上运行带有 Python 3.5.3 的 Jupyter 5.0.0 笔记本时失败并显示 `BrokenProcessPool`

coder 2023-11-12 原文

我在 Windows 10 上使用 Python 3.5.3 运行 Jupyter 5.0.0 notebook。以下示例代码无法运行:

from concurrent.futures import as_completed, ProcessPoolExecutor
import time
import numpy as np

def do_work(idx1, idx2):
    time.sleep(0.2)
    return np.mean([idx1, idx2])

with ProcessPoolExecutor(max_workers=4) as executor:
    futures = set()
    for idx in range(32):
        future = winprocess.submit(
            executor, do_work, idx, idx * 2
        )
        futures.add(future)

    for future in as_completed(futures):
        print(future.result())

... 并抛出 BrokenProcessPool:进程池中的进程在未来运行或挂起时突然终止。

代码在 Ubuntu 14.04 上运行良好。

我知道 Windows 没有 os.fork,因此多处理的处理方式不同,并且在交互模式和 Jupyter 中并不总是很好。

在这种情况下,有哪些解决方法可以使 ProcessPoolExecutor 正常工作?

还有一些类似的问题,但它们与multiprocessing.Pool有关:

最佳答案

仔细检查表明,Jupyter notebook 可以运行使用 ProcessPoolExecutor 并行化的外部 python 模块。因此,一种解决方案是在模块中执行代码的可并行化部分,然后从 Jupyter notebook 调用它。

也就是说,这可以概括为一种实用程序。以下内容可以存储为模块,例如 winprocess.py 并由 jupyter 导入。

import inspect
import types


def execute_source(callback_imports, callback_name, callback_source, args):
    for callback_import in callback_imports:
        exec(callback_import, globals())
    exec('import time' + "\n" + callback_source)
    callback = locals()[callback_name]
    return callback(*args)


def submit(executor, callback, *args):
    callback_source = inspect.getsource(callback)
    callback_imports = list(imports(callback.__globals__))
    callback_name = callback.__name__
    future = executor.submit(
        execute_source,
        callback_imports, callback_name, callback_source, args
    )
    return future


def imports(callback_globals):
    for name, val in list(callback_globals.items()):
        if isinstance(val, types.ModuleType) and val.__name__ != 'builtins' and val.__name__ != __name__:
            import_line = 'import ' + val.__name__
            if val.__name__ != name:
                import_line += ' as ' + name
            yield import_line

以下是您将如何使用它:

from concurrent.futures import as_completed, ProcessPoolExecutor
import time
import numpy as np
import winprocess

def do_work(idx1, idx2):
    time.sleep(0.2)
    return np.mean([idx1, idx2])

with ProcessPoolExecutor(max_workers=4) as executor:
    futures = set()
    for idx in range(32):
        future = winprocess.submit(
            executor, do_work, idx, idx * 2
        )
        futures.add(future)

    for future in as_completed(futures):
        print(future.result())

请注意 executor 已更改为 winprocess 并且原始 executor 被传递给 submit 函数一个参数。

这里发生的是笔记本函数代码和导入被序列化并传递给模块执行。代码只有在安全地进入新进程后才会执行,因此不会因尝试基于 jupyter notebook 本身创建新进程而失败。

以维护别名的方式处理导入。如果您确保导入在函数本身内执行的函数所需的所有内容,则可以删除导入魔法。

此外,此解决方案仅在您将所有必要变量作为参数传递给函数时才有效。可以说该函数应该是静态的,但我认为这也是 ProcessPoolExecutor 的要求。最后,确保您不执行笔记本中其他地方定义的其他功能。仅导入外部模块,因此不会包含其他笔记本功能。

关于windows - `ProcessPoolExecutor` 在 Ubuntu 上工作,但在 Windows 10 上运行带有 Python 3.5.3 的 Jupyter 5.0.0 笔记本时失败并显示 `BrokenProcessPool`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43836876/

有关windows - `ProcessPoolExecutor` 在 Ubuntu 上工作,但在 Windows 10 上运行带有 Python 3.5.3 的 Jupyter 5.0.0 笔记本时失败并显示 `BrokenProcessPool`的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  5. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  8. ruby - Sinatra:运行 rspec 测试时记录噪音 - 2

    Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/

  9. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  10. ruby - 即使失败也继续进行多主机测试 - 2

    我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r

随机推荐