Comments (2)
我昨天在mac中也遇到了这个error 看来还真的会区分系统
from python-100-days.
macOS 从 Python 3.8 开始默认使用 'spawn' 方法以避免与 Objective-C 运行时的冲突。
以下是修改后的代码,它将自动检测操作系统并相应地设置适当的进程启动方法。此外,我还添加了一些错误处理以确保主进程等待子进程完成,并适当地关闭队列。
import os
from multiprocessing import Process, Queue, set_start_method, get_start_method
from time import sleep
def sub_task(string, q):
a = q.get()
sleep(0.01)
while a < 10:
if q.empty():
q.put(a + 1)
sleep(0.01)
print(string, end='', flush=True)
a = q.get()
if q.empty():
q.put(a + 1)
sleep(0.1)
def main():
set_start_method("spawn")
q = Queue()
q.put(0)
p1 = Process(target=sub_task, args=('Ping', q))
p2 = Process(target=sub_task, args=('Pong', q))
# 启动进程
p1.start()
p2.start()
# 等待进程完成
p1.join()
p2.join()
print("\nProcesses completed.")
if __name__ == '__main__':
main()
from python-100-days.
Related Issues (20)
- ReadMe in ENGLISH HOT 3
- 💥 零基础也能hold住!轻松学会科学上网和ChatGPT
- 好
- 爬虫
- python
- 公开课中的视频分享链接失效了
- 学习
- Day13最后一个例子感觉计时部分不太对啊。。计时前是不是线程就已经开始启动了? HOT 14
- Python自学 HOT 14
- Python program to display Pyramid Pattern n=5 # number of rows # outer loop handling the number of rows for x in range(0,n): # inner loop handling the number of columns # values change according toouter loop for y in range(1,x+1): # displays the star in pyramid pattern print("*",end=" ") print()
- Python 学习
- 什么勾巴玩意,污染github环境,去隔壁gitee不好吗 HOT 3
- 利用ChatGPT快速入门Python
- python
- Starts
- 希望能添加 python的C++/ CUDA扩展开发教程 以及 python 和 C++混合调试教程【pdb + gdb】.
- 👌稳
- 一元机场官网/官方地址
- 一元机场安全吗?
- 一些数学公式显示错误。
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-100-days.