【问题标题】:Error when trying run loop with multiprocessing in python在 python 中尝试使用多处理运行循环时出错
【发布时间】:2022-12-07 12:54:29
【问题描述】:

我正在尝试使用并行多处理在循环中多次运行一个函数。

当我运行这个简单的代码时:

import time
from multiprocessing import Pool

def heavy_processing(number):
    time.sleep(0.05)  # simulate a long-running operation
    output = number + 1
    return output

with Pool(4) as p:
    numbers = list(range(0, 1000))    
    results = p.map(heavy_processing, numbers)

我收到以下错误:

Process SpawnPoolWorker-1:
Traceback (most recent call last):
  File "C:\ProgramData\Miniconda3\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "C:\ProgramData\Miniconda3\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:\ProgramData\Miniconda3\lib\multiprocessing\pool.py", line 114, in worker
    task = get()
  File "C:\ProgramData\Miniconda3\lib\multiprocessing\queues.py", line 367, in get
    return _ForkingPickler.loads(res)
AttributeError: Can't get attribute 'heavy_processing' on <module '__main__' (built-in)>

我不确定为什么,因为我几乎直接从其他来源提取了这个例子。知道发生了什么事吗?

【问题讨论】:

    标签: python multiprocessing


    【解决方案1】:

    当我尝试时,你的例子奏效了。 @Amysoj-Louis 也是正确的。您可以使用任一答案。

    import time
    from multiprocessing import Pool
    from functools import partial
    
    
    def heavy_processing(number):
        time.sleep(0.05)  # simulate a long-running operation
        output = number + 1
        return output
    
    
    # Bind the heavy_processing function to the global name `work_func`
    work_func = partial(heavy_processing)
    
    with Pool(4) as p:
        numbers = list(range(0, 1000))
        results = p.map(work_func, numbers)
    

    【讨论】:

      【解决方案2】:

      您必须始终在 if 下运行多处理代码姓名== '主要的': 或者 否则它不起作用。如果您看到 cmd 的最后一行,您会看到 AttributeError: Can't get attribute 'heavy_processing' on <module '主要的' (built-in)> 指定它在 ' 上找不到 'heavy_processing'主要的'

      完整代码

      import time
      from multiprocessing import Pool
      
      
      def heavy_processing(number):
          time.sleep(0.05)  # simulate a long-running operation
          output = number + 1
          print(output)
          return output
      
      
      if __name__ == '__main__':
          with Pool(4) as p:
              numbers = list(range(0, 1000))
              results = p.map(heavy_processing, numbers)
      

      输出

      1
      64
      127
      190
      2  
      65 
      128
      191
      3
      66
      129
      192
      4
      67
      130
      193
      5
      68
      131
      194
      6
      69
      132
      195
      7
      70
      133
      196
      8
      71
      134
      197
      9
      72
      135
      198
      10
      73
      136
      199
      11
      74
      137
      200
      75
      12
      138
      201
      76
      13
      139
      202
      77
      14
      140
      203
      78
      15
      141
      204
      79
      16
      142
      205
      17
      80
      143
      206
      18
      81
      144
      207
      19
      82
      145
      208
      20
      83
      146
      209
      21
      84
      147
      210
      22
      85
      148
      211
      23
      86
      149
      212
      24
      87
      150
      213
      25
      88
      151
      214
      26
      89
      152
      215
      90
      27
      153
      216
      91
      28
      154
      217
      92
      29
      155
      218
      30
      93
      156
      219
      31
      94
      157
      220
      95
      32
      158
      221
      33
      96
      159
      222
      97
      34
      160
      223
      98
      35
      161
      224
      99
      36
      162
      225
      100
      37
      163
      226
      101
      38
      164
      227
      102
      39
      165
      228
      103
      40
      166
      229
      104
      41
      167
      230
      105
      42
      168
      231
      106
      43
      169
      232
      107
      44
      170
      233
      108
      45
      171
      234
      109
      46
      172
      235
      110
      47
      173
      236
      111
      48
      174
      237
      112
      49
      175
      238
      113
      50
      176
      239
      114
      51
      177
      240
      52
      115
      178
      241
      53
      116
      179
      242
      117
      54
      180
      243
      55
      118
      181
      244
      56
      119
      182
      245
      57
      120
      183
      246
      121
      58
      184
      247
      122
      59
      185
      248
      123
      60
      186
      249
      124
      61
      187
      250
      125
      62
      188
      251
      126
      63
      189
      252
      253
      316
      379
      442
      254
      317
      380
      443
      255
      318
      381
      444
      256
      319
      382
      445
      257
      320
      383
      446
      258
      321
      384
      447
      259
      322
      385
      448
      260
      323
      386
      449
      261
      324
      387
      450
      262
      325
      388
      451
      263
      326
      389
      452
      264
      327
      390
      453
      265
      328
      391
      454
      266
      329
      392
      455
      267
      330
      393
      456
      268
      331
      394
      457
      269
      332
      395
      458
      270
      333
      396
      459
      271
      334
      397
      460
      272
      335
      398
      461
      273
      336
      399
      462
      274
      337
      400
      463
      275
      338
      401
      464
      276
      339
      402
      465
      277
      340
      403
      466
      278
      341
      404
      467
      279
      342
      405
      280
      468
      343
      406
      281
      469
      344
      407
      470
      282
      345
      408
      471
      283
      346
      409
      472
      284
      347
      410
      473
      285
      348
      411
      474
      286
      349
      412
      475
      287
      350
      413
      476
      288
      351
      414
      477
      289
      352
      415
      290
      478
      353
      416
      479
      291
      354
      417
      480
      292
      355
      418
      293
      481
      356
      419
      294
      482
      357
      420
      295
      483
      358
      421
      296
      484
      359
      422
      297
      485
      360
      423
      298
      486
      361
      424
      299
      487
      362
      425
      300
      488
      363
      426
      301
      489
      364
      427
      302
      490
      365
      428
      303
      491
      366
      429
      304
      492
      367
      430
      305
      493
      368
      431
      306
      494
      369
      432
      307
      495
      370
      433
      308
      496
      371
      434
      309
      497
      372
      435
      310
      498
      373
      436
      311
      499
      374
      437
      312
      500
      375
      438
      313
      501
      376
      439
      314
      502
      377
      440
      315
      503
      378
      441
      505
      504
      568
      631
      506
      694
      569
      632
      507
      695
      570
      633
      508
      696
      571
      634
      509
      697
      572
      635
      510
      698
      573
      636
      511
      699
      574
      637
      512
      700
      575
      638
      513
      701
      576
      639
      514
      702
      577
      640
      515
      703
      578
      516
      704
      641
      579
      517
      705
      642
      580
      518
      706
      643
      581
      519
      707
      644
      582
      520
      708
      645
      583
      521
      709
      646
      584
      522
      710
      647
      585
      523
      711
      648
      586
      524
      712
      649
      587
      525
      650
      713
      588
      526
      651
      714
      589
      527
      715
      652
      590
      528
      716
      653
      591
      529
      717
      654
      592
      530
      718
      655
      593
      531
      719
      656
      594
      532
      720
      657
      595
      721
      533
      658
      596
      534
      722
      659
      597
      535
      723
      660
      598
      536
      724
      661
      599
      537
      725
      662
      600
      538
      726
      663
      601
      539
      727
      664
      602
      540
      728
      665
      603
      541
      729
      666
      604
      542
      730
      667
      605
      543
      731
      668
      606
      544
      732
      669
      607
      545
      733
      670
      608
      546
      734
      671
      609
      547
      735
      672
      610
      548
      736
      673
      611
      549
      737
      674
      612
      550
      738
      675
      613
      551
      739
      676
      614
      552
      740
      677
      615
      553
      741
      678
      616
      554
      742
      679
      617
      555
      743
      680
      618
      556
      744
      681
      619
      557
      745
      682
      620
      558
      746
      683
      621
      559
      747
      684
      622
      560
      748
      685
      623
      561
      749
      686
      624
      562
      750
      687
      625
      563
      751
      688
      626
      564
      752
      689
      627
      565
      753
      690
      628
      566
      754
      691
      629
      567
      755
      692
      630
      757
      756
      693
      820
      758
      883
      946
      821
      759
      884
      947
      822
      760
      885
      948
      823
      761
      886
      949
      824
      762
      887
      950
      825
      763
      888
      951
      826
      764
      889
      952
      827
      765
      890
      953
      828
      766
      891
      954
      829
      767
      892
      955
      830
      768
      893
      956
      831
      769
      894
      957
      832
      770
      895
      958
      833
      771
      896
      959
      834
      772
      897
      960
      835
      773
      898
      961
      836
      774
      899
      962
      837
      775
      900
      963
      838
      776
      901
      964
      839
      777
      902
      965
      840
      778
      903
      966
      841
      779
      904
      967
      842
      780
      905
      968
      843
      781
      906
      969
      844
      782
      907
      970
      845
      783
      908
      971
      846
      784
      909
      972
      847
      785
      910
      973
      848
      786
      911
      974
      849
      787
      912
      975
      850
      788
      913
      976
      851
      789
      914
      977
      852
      790
      915
      978
      853
      791
      916
      979
      854
      792
      917
      980
      855
      793
      918
      981
      856
      794
      919
      982
      857
      795
      920
      983
      858
      796
      921
      984
      859
      797
      922
      985
      860
      798
      923
      986
      861
      799
      924
      987
      862
      800
      925
      988
      863
      801
      926
      989
      864
      802
      927
      990
      865
      803
      928
      991
      866
      804
      929
      992
      867
      805
      930
      993
      868
      806
      931
      994
      869
      807
      932
      995
      870
      808
      933
      996
      871
      809
      934
      997
      872
      810
      935
      998
      873
      811
      936
      999
      874
      812
      937
      1000
      875
      813
      938
      876
      814
      939
      877
      815
      940
      878
      816
      941
      879
      817
      942
      880
      818
      943
      881
      819
      944
      882
      945
      

      希望这可以帮助。快乐编码:)

      【讨论】:

        猜你喜欢
        • 2021-09-22
        • 2020-06-14
        • 1970-01-01
        • 1970-01-01
        • 2021-03-12
        • 2017-11-19
        • 1970-01-01
        • 2020-05-27
        • 1970-01-01
        相关资源
        最近更新 更多