Python3 in _check_not_importing_main is not going to be frozen to produce an executable.''')

2024-04-20 18:08

本文主要是介绍Python3 in _check_not_importing_main is not going to be frozen to produce an executable.'''),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

    

    知道自己知道什么,也知道自己不知道什么,这就是真正的知识。--------梭罗


   在进行python3多进程multiprocessing学习过程中,在Windows环境下测试分布式代码过程中,利用fork()生成child processes的代码须放在main模块中,否则将会报错:

    代码如下:

import queue
import random
from multiprocessing.managers import BaseManagertask_queue = queue.Queue()
result_queue = queue.Queue()def return_task_queue():global task_queuereturn task_queuedef return_result_queue():global result_queuereturn result_queueclass QueueManager(BaseManager):passQueueManager.register('get_task_queue', callable=return_task_queue)
QueueManager.register('get_result_queue', callable=return_result_queue)manager = QueueManager(address=('127.0.0.1', 5000), authkey=b'abc')
manager.start()task = manager.get_task_queue()
result = manager.get_result_queue()
for i in range(10):n = random.randint(0, 10000)print('Put task %d' % n)task.put(n)print('Try get results..')
for i in range(10):r = result.get(timeout=10)print('Result:%s' % r)manager.shutdown()
print('master exit.')
    报错信息:

"E:\python\python project\myfirst\venv\Scripts\python.exe" "E:/python/python project/myfirst/vari/distibuted_master.py"
Traceback (most recent call last):File "<string>", line 1, in <module>File "D:\program files\Python3.6\Lib\multiprocessing\spawn.py", line 105, in spawn_mainexitcode = _main(fd)File "D:\program files\Python3.6\Lib\multiprocessing\spawn.py", line 114, in _mainprepare(preparation_data)File "D:\program files\Python3.6\Lib\multiprocessing\spawn.py", line 225, in prepare_fixup_main_from_path(data['init_main_from_path'])File "D:\program files\Python3.6\Lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_pathrun_name="__mp_main__")File "D:\program files\Python3.6\Lib\runpy.py", line 263, in run_pathpkg_name=pkg_name, script_name=fname)File "D:\program files\Python3.6\Lib\runpy.py", line 96, in _run_module_codemod_name, mod_spec, pkg_name, script_name)File "D:\program files\Python3.6\Lib\runpy.py", line 85, in _run_codeexec(code, run_globals)File "E:\python\python project\myfirst\vari\distibuted_master.py", line 24, in <module>manager.start()File "D:\program files\Python3.6\Lib\multiprocessing\managers.py", line 513, in startself._process.start()File "D:\program files\Python3.6\Lib\multiprocessing\process.py", line 105, in startself._popen = self._Popen(self)File "D:\program files\Python3.6\Lib\multiprocessing\context.py", line 322, in _Popenreturn Popen(process_obj)File "D:\program files\Python3.6\Lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__prep_data = spawn.get_preparation_data(process_obj._name)File "D:\program files\Python3.6\Lib\multiprocessing\spawn.py", line 143, in get_preparation_data_check_not_importing_main()File "D:\program files\Python3.6\Lib\multiprocessing\spawn.py", line 136, in _check_not_importing_mainis not going to be frozen to produce an executable.''')
RuntimeError: An attempt has been made to start a new process before thecurrent process has finished its bootstrapping phase.This probably means that you are not using fork to start yourchild processes and you have forgotten to use the proper idiomin the main module:if __name__ == '__main__':freeze_support()...The "freeze_support()" line can be omitted if the programis not going to be frozen to produce an executable.Process finished with exit code 1
    所以需要调整代码,把相应调用代码放在main模块中:

import queue
import random
from multiprocessing.managers import BaseManagertask_queue = queue.Queue()
result_queue = queue.Queue()def return_task_queue():global task_queuereturn task_queuedef return_result_queue():global result_queuereturn result_queueclass QueueManager(BaseManager):passif __name__=='__main__':QueueManager.register('get_task_queue', callable=return_task_queue)QueueManager.register('get_result_queue', callable=return_result_queue)manager = QueueManager(address=('127.0.0.1', 5000), authkey=b'abc')manager.start()task = manager.get_task_queue()result = manager.get_result_queue()for i in range(10):n = random.randint(0, 10000)print('Put task %d' % n)task.put(n)print('Try get results..')for i in range(10):r = result.get(timeout=10)print('Result:%s' % r)manager.shutdown()print('master exit.')
    结果如下:

"E:\python\python project\myfirst\venv\Scripts\python.exe" "E:/python/python project/myfirst/vari/distibuted_master.py"
Put task 5292
Put task 5986
Put task 3500
Put task 2385
Put task 8343
Put task 6482
Put task 4331
Put task 6104
Put task 9341
Put task 5156
Try get results..Process finished with exit code 1

喜欢的朋友可以扫描以下二维码进行关注,公众号将每天更新文章:





这篇关于Python3 in _check_not_importing_main is not going to be frozen to produce an executable.''')的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/920959

相关文章

Python3 BeautifulSoup爬虫 POJ自动提交

POJ 提交代码采用Base64加密方式 import http.cookiejarimport loggingimport urllib.parseimport urllib.requestimport base64from bs4 import BeautifulSoupfrom submitcode import SubmitCodeclass SubmitPoj():de

SOMEIP_ETS_095: SD_Check_subscribe_eventgroup_ttl_expired

测试目的: 验证DUT(Device Under Test)能够检测到测试器(Tester)的订阅已过期(ttl = 3秒),并且在TTL过期后不响应测试器触发的事件。 描述 本测试用例旨在确保DUT能够识别测试器的订阅已过期,并在订阅过期后不响应测试器通过TriggerEventUint8方法触发的事件。 测试拓扑: 具体步骤: TESTER:发送订阅事件组消息,用于事件组0x0

跟我一起玩《linux内核设计的艺术》第1章(四)——from setup.s to head.s,这回一定让main滚出来!(已解封)

看到书上1.3的大标题,以为马上就要见着main了,其实啊,还早着呢,光看setup.s和head.s的代码量就知道,跟bootsect.s没有可比性,真多……这确实需要包括我在内的大家多一些耐心,相信见着main后,大家的信心和干劲会上一个台阶,加油! 既然上篇已经玩转gdb,接下来的讲解肯定是边调试边分析书上的内容,纯理论讲解其实我并不在行。 setup.s: 目标:争取把setup.

main函数执行前、后再执行的代码

一、main结束 不代表整个进程结束  (1)全局对象的构造函数会在main 函数之前执行,          全局对象的析构函数会在main函数之后执行;          用atexit注册的函数也会在main之后执行。  (2)一些全局变量、对象和静态变量、对象的空间分配和赋初值就是在执行main函数之前,而main函数执行完后,还要去执行一些诸如释放空间、释放资源使用权等操作   (3)

Python安装:Mac 使用brew 安装Python2 和 Python3

安装python ## python2brew install python ## python3brew install python3 出现错误 Error: An unexpected error occurred during the `brew link` stepThe formula built, but is not symlinked into /usr/loc

Python: #!/usr/bin/python3 #!/usr/bin/env python3

只能放在第一行,第二行就没有效果了。 1. 路径不同 #!/usr/bin/python3&& #!/usr/bin/env python3写在脚本语言第一行的目的是 想要以什么可执行程序去运行这个文件中的代码。 #!/usr/bin/python3是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python3解释器; #!/usr/bin/env python3这种用法是为了

Linux搭建Python3、Django环境

开发十年,就只剩下这套架构体系了! >>>    好久没写了,朋友们,我又回来了。 安装Python3 Python全部版本下载地址:         https://www.python.org/ftp/ 解决RedHat,使用Python3退格出现乱码问题:         yum -y install readline-devel.x86_64 下载Python3:

ubuntu 安装python3 教程

本篇教程,主要介绍如何在Ubuntu上安装python3教程。 1、查看是否有python 在安装前,首先看看自己系统上,是否存在python环境,可能有些系统,默认就安装过python,如果已经有python了,可以直接跳过安装教程。 2、安装步骤 apt update && apt install -y python3 python3-pip

【python requests警告】python3.x requests库取消ssl验证,InsecureRequestWarning: Unverified HTTPS request is be

警告信息: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warni

设置python4delphi支持python3.X

看不懂也要跟踪下,哪哪出问题了,嗯,是python的Py_Initialize()出问题,这行代码   Py_Initialize             := Import('Py_Initialize'); 咋办呢,万能的互联网啊(哪天不能上网,那是不是也活的差不多了) 网上说,一般出这个问题,是python的路径出问题了,系统环境变量里找不着python了,哦,是这样,但是我不能设置p