python多进程程序打包成exe的问题
作者:野生大虾 时间:2023-08-07 13:39:03
粘贴一下部分的多进程代码
if __name__ == '__main__':
""""流程模拟"""
multiprocessing.freeze_support() # 打包成exe时,需要该语句,防止系统无限创建子线程
print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} - [主进程] - 程序已启动")
if WORKERS > 0:
print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} - [主进程] - 当前为并发爬取,每次采集{WORKERS}条url")
else:
print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} - [主进程] - 当前为单线程爬取,时间间隔为{INTERVAL}秒")
init_data_queue = multiprocessing.Queue() # 从数据库里取出来的需要爬取的url等信息
result_data_queue = multiprocessing.Queue() # 爬取的结果
wait_to_insert_queue = multiprocessing.Queue() # 等待写入数据库的数据 已经去重 符合insert的格式要求
p1 = multiprocessing.Process(target=get_remote_data, args=(init_data_queue, wait_to_insert_queue), name='[数据库交互进程]')
p2 = multiprocessing.Process(target=scrapy_wechat, args=(init_data_queue, result_data_queue), name='[爬虫进程]')
p3 = multiprocessing.Process(target=make_data, args=(result_data_queue, wait_to_insert_queue), name='[数据处理进程]')
try:
p1.start()
p2.start()
p3.start()
except Exception as e:
print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} - [主进程] - 错误信息: {e}")
程序打包成exe文件后,启动时会疯狂创建子进程,程序来回在if WORKERS > 0:
这行代码中执行,即无法正常工作,还会导致电脑逐渐卡死。解决的办法为:在程序入口前,加入multiprocessing.freeze_support()
即可解决此事。值得注意的时,该语必须写在if __name__ == "__main__"
的下一句,如果没有main语句,也会出错。
来源:https://www.cnblogs.com/mooremok/p/16930929.html
标签:python打包exe
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
定格动画浅析(一)
2009-07-30 12:50:00
![](https://img.aspxhome.com/file/UploadPic/20097/30/0907_dingge-1_08-67s.jpg)
对numpy中数组元素的统一赋值实例
2021-05-31 18:47:37
JavaScript奇怪的比较——隐式类型转换
2009-02-15 13:06:00
实现动画效果核心方式的js代码
2024-04-19 10:45:39
Python中的pack和unpack的使用
2023-08-20 05:24:33
Python玩转Excel的读写改实例
2022-01-27 19:59:47
python使用matplotlib绘制折线图的示例代码
2021-06-25 22:51:18
![](https://img.aspxhome.com/file/2023/9/101649_0s.png)
Vue中的baseurl如何配置
2024-05-09 15:10:46
Python简单实现词云图代码及步骤解析
2021-05-13 00:25:52
![](https://img.aspxhome.com/file/2023/7/93517_0s.png)
fastapi与django异步的并发对比分析
2023-01-03 19:49:30
![](https://img.aspxhome.com/file/2023/6/121006_0s.jpg)
OpenCV利用python来实现图像的直方图均衡化
2023-01-22 15:05:36
![](https://img.aspxhome.com/file/2023/2/82582_0s.jpg)
python re模块findall()函数实例解析
2022-07-07 13:38:40
Python处理mysql特殊字符的问题
2024-01-17 01:28:38
Python函数中定义参数的四种方式
2021-10-11 03:10:20
python 如何上传包到pypi
2023-12-22 13:31:41
使用virtualenv创建Python环境及PyQT5环境配置的方法
2022-12-30 06:09:26
![](https://img.aspxhome.com/file/2023/2/105262_0s.png)
浅析Python 条件控制语句
2023-08-31 02:59:46
![](https://img.aspxhome.com/file/2023/0/117940_0s.jpg)
PHP检查端口是否可以被绑定的方法示例
2024-05-13 09:26:32
SqlServer 2005/2008数据库被标记为“可疑”的解决办法
2024-01-18 14:07:14
Pytorch:Conv2d卷积前后尺寸详解
2021-01-18 23:36:54
![](https://img.aspxhome.com/file/2023/0/118560_0s.png)