Python 在创建多进程时抛出RuntimeError错误

一、错误信息

RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
  if name == ‘main‘:
    freeze_support()
    …
The “freeze_support()” line can be omitted if the program
is not going to be frozen to produce an executable.

二、出错代码

from multiprocessing import Pool
import os
import time
import random


def test(a1):
    t_start = time.time()
    print("%s 开始执行,进程号为%d" % (a1, os.getpid()))
    time.sleep(random.random() * 2)
    t_stop = time.time()
    print("%s 执行完毕,耗时%.2f" % (a1, (t_stop - t_start)))

p1 = Pool(3)
for i in range(0, 10):
    p1.apply_async(test, args=(i,))

print("-----start-----")
p1.close()
p1.join()
print("------end------")

三、错误原因

Python 解释器在 Windows平台 执行创建多进程的程序时,子进程会读取当前 Python 文件,用以创建进程。
在子进程读取当前文件时,读取到创建子进程的代码时又会创建新的子进程,这样程序就陷入递归创建进程的状态。
由于 Python 解释器中对于创建子进程有一个最大数量限制来保护我们的计算机(为龟叔点赞 😄),防止其内存溢出,因此程序会抛出异常。

四、改正方法

将程序创建子进程的放进 if __name__ == '__main__': 语句内,该语句的作用是判断当前进程是否为主进程,是主进程才执行程序。

五、改进后的代码

from multiprocessing import Pool
import os
import time
import random


def test(a1):
    t_start = time.time()
    print("%s 开始执行,进程号为%d" % (a1, os.getpid()))
    time.sleep(random.random() * 2)
    t_stop = time.time()
    print("%s 执行完毕,耗时%.2f" % (a1, (t_stop - t_start)))

if __name__=='__main__':
    p1 = Pool(3)
    for i in range(0, 10):
        p1.apply_async(test, args=(i,))

    print("-----start-----")
    p1.close()
    p1.join()
    print("------end------")
  • 16
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
THIS BOOK IS DIFFERENT FROM THE OTHERS. CRITICAL POINT: Don't trust Only Theoretical Manuals that don't let you do any Practical Exercises: Practical Exercises are Essential! FIRST POINT: This is a Practical Guide. Step by Step. Not only Theory but You can do the Exercises and really understand the Ethical Hacking. SECOND POINT: There are Dozens of Screenshots and Images that allow you to understand, step by step, what you are doing. THIRD POINT: The Author of the book, Jim Kou, has Twenty Years of experience in the CyberSecurity and Ethical Hacking subjects. FOURTH POINT: The only way to become a Penetration Tester is through Experience and Practical Exercises. FIFTH POINT: In this Manual, we'll start with the Basics. 2019 UPDATED! You will learn: CHAPTER 1: INTRODUCTION TO ETHICAL HACKING In this chapter, I will try to explain to you how an ethical hacker works, what are his goals and the working method you should follow to become one. CHAPTER 2: THE LABORATORY The first task is to build our own laboratory. CHAPTER 3: LINUX COMMANDS These are the most common commands that you will probably use for your routine tasks. CHAPTER 4: MIND MAPS During your work as a penetration tester, you will collect a great deal of information that needs to be organized efficiently. CHAPTER 5: NETWORK THEORY This chapter aims to give you an overview of the main services and network protocols. CHAPTER 6: CORPORATE NETWORKS Having a good understanding of most network devices and their functions will help you carry out a penetration test in a much more accurate and effective way. CHAPTER 7: INFORMATION GATHERING Gathering information means investigating, analyzing, and studying everything related to our target. CHAPTER 8: NETWORK SCANNING What exactly do I mean with “scanning”? Each of these IP addresses will expose a certain service/port to the outside world. CHAPTER 9: BANNER GRABBING Now it's time to identify what type of service is running on a specific port. CHAPTER 10: ENUMERATION It consists in exploiting the characteristics of a certain service in order to obtain as much information as possible. CHAPTER 11: VULNERABILITY ASSESSMENT Now it's time to look for any vulnerabilities and we will use specific tools to carry out this activity. CHAPTER 12: EXPLOITATION Exploitation is meant to confirm if we can access our target machine from a given vulnerability. CHAPTER 13: POST-EXPLOITATION Now that the host has been compromised, we need to look into all the activities that we should carry out after the exploitation CHAPTER 14: THE FINAL REPORT

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值