pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      <bdo id='YUHAT'></bdo><ul id='YUHAT'></ul>
    <i id='YUHAT'><tr id='YUHAT'><dt id='YUHAT'><q id='YUHAT'><span id='YUHAT'><b id='YUHAT'><form id='YUHAT'><ins id='YUHAT'></ins><ul id='YUHAT'></ul><sub id='YUHAT'></sub></form><legend id='YUHAT'></legend><bdo id='YUHAT'><pre id='YUHAT'><center id='YUHAT'></center></pre></bdo></b><th id='YUHAT'></th></span></q></dt></tr></i><div class="c8dfl7c" id='YUHAT'><tfoot id='YUHAT'></tfoot><dl id='YUHAT'><fieldset id='YUHAT'></fieldset></dl></div>

    <small id='YUHAT'></small><noframes id='YUHAT'>

      <legend id='YUHAT'><style id='YUHAT'><dir id='YUHAT'><q id='YUHAT'></q></dir></style></legend>
      <tfoot id='YUHAT'></tfoot>

        多處理:我怎樣才能 ???????? 從子進程重定

        multiprocessing: How can I ???????? redirect stdout from a child process?(多處理:我怎樣才能 ???????? 從子進程重定向標準輸出?)
            <tbody id='3dDTX'></tbody>

          1. <i id='3dDTX'><tr id='3dDTX'><dt id='3dDTX'><q id='3dDTX'><span id='3dDTX'><b id='3dDTX'><form id='3dDTX'><ins id='3dDTX'></ins><ul id='3dDTX'></ul><sub id='3dDTX'></sub></form><legend id='3dDTX'></legend><bdo id='3dDTX'><pre id='3dDTX'><center id='3dDTX'></center></pre></bdo></b><th id='3dDTX'></th></span></q></dt></tr></i><div class="tfpru3f" id='3dDTX'><tfoot id='3dDTX'></tfoot><dl id='3dDTX'><fieldset id='3dDTX'></fieldset></dl></div>

            <small id='3dDTX'></small><noframes id='3dDTX'>

                <bdo id='3dDTX'></bdo><ul id='3dDTX'></ul>
              • <tfoot id='3dDTX'></tfoot>
              • <legend id='3dDTX'><style id='3dDTX'><dir id='3dDTX'><q id='3dDTX'></q></dir></style></legend>

                • 本文介紹了多處理:我怎樣才能 ???????? 從子進程重定向標準輸出?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  注意.我見過 multiprocessing.Process 的日志輸出 - 不幸的是,它沒有回答這個問題問題.

                  NB. I have seen Log output of multiprocessing.Process - unfortunately, it doesn't answer this question.

                  我正在通過多處理創(chuàng)建一個子進程(在 Windows 上).我希望將子進程的 stdout 和 stderr 輸出的 all 重定向到日志文件,而不是出現(xiàn)在控制臺上.我看到的唯一建議是讓子進程將 sys.stdout 設(shè)置為文件.但是,由于 Windows 上的標準輸出重定向行為,這并不能有效地重定向所有標準輸出輸出.

                  I am creating a child process (on windows) via multiprocessing. I want all of the child process's stdout and stderr output to be redirected to a log file, rather than appearing at the console. The only suggestion I have seen is for the child process to set sys.stdout to a file. However, this does not effectively redirect all stdout output, due to the behaviour of stdout redirection on Windows.

                  為了說明問題,使用以下代碼構(gòu)建一個 Windows DLL

                  To illustrate the problem, build a Windows DLL with the following code

                  #include <iostream>
                  
                  extern "C"
                  {
                      __declspec(dllexport) void writeToStdOut()
                      {
                          std::cout << "Writing to STDOUT from test DLL" << std::endl;
                      }
                  }
                  

                  然后創(chuàng)建并運行如下 python 腳本,它會導(dǎo)入這個 DLL 并調(diào)用函數(shù):

                  Then create and run a python script like the following, which imports this DLL and calls the function:

                  from ctypes import *
                  import sys
                  
                  print
                  print "Writing to STDOUT from python, before redirect"
                  print
                  sys.stdout = open("stdout_redirect_log.txt", "w")
                  print "Writing to STDOUT from python, after redirect"
                  
                  testdll = CDLL("Release/stdout_test.dll")
                  testdll.writeToStdOut()
                  

                  為了看到與我相同的行為,可能需要針對不同于 Python 使用的 C 運行時構(gòu)建 DLL.在我的例子中,python 是用 Visual Studio 2010 構(gòu)建的,但我的 DLL 是用 VS 2005 構(gòu)建的.

                  In order to see the same behaviour as me, it is probably necessary for the DLL to be built against a different C runtime than than the one Python uses. In my case, python is built with Visual Studio 2010, but my DLL is built with VS 2005.

                  我看到的行為是控制臺顯示:

                  The behaviour I see is that the console shows:

                  > stdout_test.py
                  
                  Writing to STDOUT from python, before redirect
                  
                  Writing to STDOUT from test DLL
                  

                  雖然文件 stdout_redirect_log.txt 最終包含:

                  While the file stdout_redirect_log.txt ends up containing:

                  Writing to STDOUT from python, after redirect
                  

                  換句話說,設(shè)置 sys.stdout 無法重定向 DLL 生成的 stdout 輸出.鑒于 Windows 中用于標準輸出重定向的底層 API 的性質(zhì),這不足為奇.我之前在本機/C++ 級別遇到過這個問題,但從未找到一種方法來可靠地從進程中重定向標準輸出.它必須在外部完成.

                  In other words, setting sys.stdout failed to redirect the stdout output generated by the DLL. This is unsurprising given the nature of the underlying APIs for stdout redirection in Windows. I have encountered this problem at the native/C++ level before and never found a way to reliably redirect stdout from within a process. It has to be done externally.

                  這實際上是我啟動子進程的原因 - 這樣我就可以從外部連接到它的管道,從而保證我正在攔截它的所有輸出.我絕對可以通過使用 pywin32 手動啟動進程來做到這一點,但我非常希望能夠使用多處理的功能,特別是通過多處理 Pipe 對象與子進程通信的能力,以獲得進展更新.問題是是否有任何方法既可以為其 IPC 設(shè)施使用多處理可靠地將所有孩子的 stdout 和 stderr 輸出重定向到一個文件.

                  This is actually the very reason I am launching a child process - it's so that I can connect externally to its pipes and thus guarantee that I am intercepting all of its output. I can definitely do this by launching the process manually with pywin32, but I would very much like to be able to use the facilities of multiprocessing, in particular the ability to communicate with the child process via a multiprocessing Pipe object, in order to get progress updates. The question is whether there is any way to both use multiprocessing for its IPC facilities and to reliably redirect all of the child's stdout and stderr output to a file.

                  更新:查看 multiprocessing.Processs 的源代碼,它有一個靜態(tài)成員 _Popen,看起來它可以用來覆蓋用于創(chuàng)建進程的類.如果設(shè)置為 None (默認),它使用一個 multiprocessing.forking._Popen,但它看起來像說

                  UPDATE: Looking at the source code for multiprocessing.Processs, it has a static member, _Popen, which looks like it can be used to override the class used to create the process. If it's set to None (default), it uses a multiprocessing.forking._Popen, but it looks like by saying

                  multiprocessing.Process._Popen = MyPopenClass
                  

                  我可以覆蓋進程創(chuàng)建.然而,雖然我可以從 multiprocessing.forking._Popen 中得到它,但看起來我必須將一堆內(nèi)部的東西復(fù)制到我的實現(xiàn)中,這聽起來很不穩(wěn)定,而且不是很面向未來.如果這是唯一的選擇,我想我可能會更愿意用 pywin32 手動完成整個工作.

                  I could override the process creation. However, although I could derive this from multiprocessing.forking._Popen, it looks like I would have to copy a bunch of internal stuff into my implementation, which sounds flaky and not very future-proof. If that's the only choice I think I'd probably plump for doing the whole thing manually with pywin32 instead.

                  推薦答案

                  您建議的解決方案是一個不錯的解決方案:手動創(chuàng)建您的進程,以便您可以顯式訪問它們的 stdout/stderr 文件句柄.然后,您可以創(chuàng)建一個套接字與子進程通信,并在該套接字上使用 multiprocessing.connection(multiprocessing.Pipe 創(chuàng)建相同類型的連接對象,因此這應(yīng)該為您提供所有相同的 IPC 功能).

                  The solution you suggest is a good one: create your processes manually such that you have explicit access to their stdout/stderr file handles. You can then create a socket to communicate with the sub-process and use multiprocessing.connection over that socket (multiprocessing.Pipe creates the same type of connection object, so this should give you all the same IPC functionality).

                  這是一個包含兩個文件的示例.

                  Here's a two-file example.

                  ma??ster.py:

                  import multiprocessing.connection
                  import subprocess
                  import socket
                  import sys, os
                  
                  ## Listen for connection from remote process (and find free port number)
                  port = 10000
                  while True:
                      try:
                          l = multiprocessing.connection.Listener(('localhost', int(port)), authkey="secret")
                          break
                      except socket.error as ex:
                          if ex.errno != 98:
                              raise
                          port += 1  ## if errno==98, then port is not available.
                  
                  proc = subprocess.Popen((sys.executable, "subproc.py", str(port)), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                  
                  ## open connection for remote process
                  conn = l.accept()
                  conn.send([1, "asd", None])
                  print(proc.stdout.readline())
                  

                  subproc.py:

                  import multiprocessing.connection
                  import subprocess
                  import sys, os, time
                  
                  port = int(sys.argv[1])
                  conn = multiprocessing.connection.Client(('localhost', port), authkey="secret")
                  
                  while True:
                      try:
                          obj = conn.recv()
                          print("received: %s
                  " % str(obj))
                          sys.stdout.flush()
                      except EOFError:  ## connection closed
                          break
                  

                  您可能還想查看 this問題 從子進程中獲取非阻塞讀取.

                  You may also want to see the first answer to this question to get non-blocking reads from the subprocess.

                  這篇關(guān)于多處理:我怎樣才能 ???????? 從子進程重定向標準輸出?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                  Passing multiple parameters to pool.map() function in Python(在 Python 中將多個參數(shù)傳遞給 pool.map() 函數(shù))
                  multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEncodingError: TypeError(cannot serialize _io.BufferedReader object,)) - IT屋-程序員軟件開
                  Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多進程池.當其中一個工作進程確定不再需要完成工作時,如何退出腳本?) - IT屋-程序員
                  How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊列引用傳遞給 pool.map_async() 管理的函數(shù)?)
                  yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(與多處理錯誤的另一個混淆,“模塊對象沒有屬性“f)

                  <legend id='Aeygb'><style id='Aeygb'><dir id='Aeygb'><q id='Aeygb'></q></dir></style></legend>
                      <i id='Aeygb'><tr id='Aeygb'><dt id='Aeygb'><q id='Aeygb'><span id='Aeygb'><b id='Aeygb'><form id='Aeygb'><ins id='Aeygb'></ins><ul id='Aeygb'></ul><sub id='Aeygb'></sub></form><legend id='Aeygb'></legend><bdo id='Aeygb'><pre id='Aeygb'><center id='Aeygb'></center></pre></bdo></b><th id='Aeygb'></th></span></q></dt></tr></i><div class="wvkmty3" id='Aeygb'><tfoot id='Aeygb'></tfoot><dl id='Aeygb'><fieldset id='Aeygb'></fieldset></dl></div>
                      <tfoot id='Aeygb'></tfoot>
                    1. <small id='Aeygb'></small><noframes id='Aeygb'>

                            <tbody id='Aeygb'></tbody>
                            <bdo id='Aeygb'></bdo><ul id='Aeygb'></ul>
                            主站蜘蛛池模板: 青岛球场围网,青岛车间隔离网,青岛机器人围栏,青岛水源地围网,青岛围网,青岛隔离栅-青岛晟腾金属制品有限公司 | ISO9001认证咨询_iso9001企业认证代理机构_14001|18001|16949|50430认证-艾世欧认证网 | 山东聚盛新型材料有限公司-纳米防腐隔热彩铝板和纳米防腐隔热板以及钛锡板、PVDF氟膜板供应商 | 碳纤维布-植筋胶-灌缝胶-固特嘉加固材料公司 | 氢氧化钙设备_厂家-淄博工贸有限公司| 亿诺千企网-企业核心产品贸易| 风信子发稿-专注为企业提供全球新闻稿发布服务| 玻璃钢格栅盖板|玻璃钢盖板|玻璃钢格栅板|树篦子-长沙川皖玻璃钢制品有限公司 | 双工位钻铣攻牙机-转换工作台钻攻中心-钻铣攻牙机一体机-浙江利硕自动化设备有限公司 | 齿轮减速机_齿轮减速电机-VEMT蜗轮蜗杆减速机马达生产厂家瓦玛特传动瑞环机电 | 过跨车_过跨电瓶车_过跨转运车_横移电动平车_厂区转运车_无轨转运车 | 工程管道/塑料管材/pvc排水管/ppr给水管/pe双壁波纹管等品牌管材批发厂家-河南洁尔康建材 | 深圳办公室装修-写字楼装修设计-深圳标榜装饰公司 | 重庆磨床过滤机,重庆纸带过滤机,机床伸缩钣金,重庆机床钣金护罩-重庆达鸿兴精密机械制造有限公司 | 高考志愿规划师_高考规划师_高考培训师_高报师_升学规划师_高考志愿规划师培训认证机构「向阳生涯」 | 辽宁资质代办_辽宁建筑资质办理_辽宁建筑资质延期升级_辽宁中杭资质代办 | 气象监测系统_气象传感器_微型气象仪_气象环境监测仪-山东风途物联网 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 动库网动库商城-体育用品专卖店:羽毛球,乒乓球拍,网球,户外装备,运动鞋,运动包,运动服饰专卖店-正品运动品网上商城动库商城网 - 动库商城 | 创富网-B2B网站|供求信息网|b2b平台|专业电子商务网站 | 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | 广州展览制作|展台制作工厂|展览设计制作|展览展示制作|搭建制作公司 | 青岛美佳乐清洁工程有限公司|青岛油烟管道清洗|酒店|企事业单位|学校工厂厨房|青岛油烟管道清洗 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 广州迈驰新GMP兽药包装机首页_药品包装机_中药散剂包装机 | 首页|专注深圳注册公司,代理记账报税,注册商标代理,工商变更,企业400电话等企业一站式服务-慧用心 | 膜结构_ETFE膜结构_膜结构厂家_膜结构设计-深圳市烨兴智能空间技术有限公司 | 产业规划_产业园区规划-产业投资选址及规划招商托管一体化服务商-中机院产业园区规划网 | 尚为传动-专业高精密蜗轮蜗杆,双导程蜗轮蜗杆,蜗轮蜗杆减速机,蜗杆减速机生产厂家 | 二氧化碳/活性炭投加系统,次氯酸钠发生器,紫外线消毒设备|广州新奥 | 金属切削液-脱水防锈油-电火花机油-抗磨液压油-深圳市雨辰宏业科技发展有限公司 | 土壤检测仪器_行星式球磨仪_土壤团粒分析仪厂家_山东莱恩德智能科技有限公司 | 沈飞防静电地板__机房地板-深圳市沈飞防静电设备有限公司 | 沈阳网站建设_沈阳网站制作_沈阳网页设计-做网站就找示剑新零售 沈阳缠绕膜价格_沈阳拉伸膜厂家_沈阳缠绕膜厂家直销 | 南京PVC快速门厂家南京快速卷帘门_南京pvc快速门_世界500强企业国内供应商_南京美高门业 | 成都中天自动化控制技术有限公司| 深圳高新投三江工业消防解决方案提供厂家_服务商_园区智慧消防_储能消防解决方案服务商_高新投三江 | 杰福伦_磁致伸缩位移传感器_线性位移传感器-意大利GEFRAN杰福伦-河南赉威液压科技有限公司 | 智能门锁电机_智能门锁离合器_智能门锁电机厂家-温州劲力智能科技有限公司 | 砂磨机_立式纳米砂磨机_实验室砂磨机-广州儒佳化工设备厂家 | 合肥宠物店装修_合肥宠物美容院装修_合肥宠物医院设计装修公司-安徽盛世和居装饰 | 仓储笼_仓储货架_南京货架_仓储货架厂家_南京货架价格低-南京一品仓储设备制造公司 |