小学社会310485 views
高校国語788336 views
世界の国564520 views
Computer368106 views
高校生物551766 views
教育149510 views
中学英語811710 views
いろは3010814 views
LaTeX962153 views
英語613639 views
Help
Tools
NewsSpreadsheetCalendarBookkeepingMarkdown TablesLanguage Model NewsSlidesTier ListPen ToolIllustrationCrayonWatercolorPixel ArtASCII ArtPerspectiveEndless StairsGraphMind MapER DiagramFamily TreeMemeCurved TextImage EditorMosaicRetro FilterPencil SketchSwirl EffectLine ArtOCR/HighlighterMakeup EditorFaviconVideo TrimmerScrolling VideoVideo TitleColor PickerColor ExtractorBonfireFireworksCherry BlossomWater RippleWater SplashBreaking GlassGlass TextureFabric TextureWood GrainMarble TextureBrick Wall TextureMetal TextureWashi Paper TextureCardboard TextureCSS ButtonIcon MakerBar ChartGrouped Bar ChartStacked Bar ChartPie ChartLine ChartArea ChartStacked Area ChartScatter Plot3D Bar Chart3D Pie ChartBar Chart RaceBubble ChartPopulation PyramidPictogramEarningsCandlestick ChartInvestment RiskMortgage SimulatorCalculatorMatrix CalculatorFunction GraphPolynomial ExpansionVenn DiagramField VisualizerRubik's Cube Group TheoryTraveling SalesmanVoronoi and DelaunayFractalColumn ArithmeticDraw Math FiguresArithmetic AnimationArithmetic Word ProblemsCounting with Tree DiagramsCube NetsRolling DiceCross SectionsMotion PathMechanicsWavesElectromagnetic WavesCapacitorsLight and LensesThermodynamicsHow Semiconductors WorkMolecular StructuresAtomic OrbitalsElectrochemical CellsChemical EquilibriumCrystal LatticesBuffer pHOrganic Reaction MapPeriodic TableComplex IonsDNA Double HelixCell DivisionMembrane ChannelsNerve ImpulseMuscle ContractionHormones and HomeostasisRock ClassificationWeatherConstellationsSolar and Lunar Eclipses3D ModelingFloor PlanSeismic StructuresIntersection TurnMaglevCooking AnimationOrigamiLive Viewer CountGeoJSON MapRailway MapPopulation MapCrime MapLand Price MapSchool MapShrine and Castle MapHouse of Representatives MapWord MapSolitaireReversiHakoiri MusumeChessHamburgerRippleSlide Puzzle MakerNeon PinballNovel MakerJapanese Typing PracticePiano Score EditorMusic TheoryShogi StrategyPiano Rhythm Game

English

Python の ThreadPoolExecutor は max_workers を適切に指定しないとゾンビのプロセスが生まれるかもしれない

注意:この記事は情報に誤りがあるかもしれない

Python の ThreadPoolExecutor は基本的に with 内で動かすが、実際のスレッド数を超える値を max_workers に指定すると予想外のことが起きる。

例えば 3 つのプロセスを並列化したいとき、max_workers を 4 にすると、4 - 3 = 1 のゾンビ・プロセスが生まれる。全体のプログラムがデーモン化されている場合、この 1 個のスレッドが with で回収されない。

例えば gunicorn で動かす Django または Flask のプログラム内に ThreadPoolExecutor で並列化した関数があるとする。この ThreadPoolExecutor で max_workers を関数の数よりも多く設定していると

13474  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13510  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13511  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13512  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13513  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13514  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13549  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13550  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13551  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13552  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13553  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13578  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13579  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13580  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13581  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13582  gunicorn --bind 127.0.0.1:8080 main:app --daemon
13586  gunicorn --bind 127.0.0.1:8080 main:app --daemon

などとなる。これらのゾンビ・デーモンはシグナルで強制終了されない限り消えない。

デーモンになっていないプログラム、例えばローカルで動かしている Flask では max_workers の値にかかわらず、余ったスレッドは with で消される(たぶん)。

また ThreadPoolExecutor は OS に依存するため、実際は Pebble などを使う。Python の並列化でシグナルを直接扱っているソースコードをよく見かけるが、たぶん避けたほうがいい。