Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
ItJava
Python24
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Python24
/
03Thread
/
day02
/
basic02.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
57 lines (42 loc) · 1.44 KB
master
Breadcrumbs
Python24
/
03Thread
/
day02
/
basic02.py
Copy path
Top
File metadata and controls
Code
Blame
57 lines (42 loc) · 1.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
多个进程之间的全局变量的共享问题
1- 多进程的执行顺序和线程一样是乱序的
2- 每个进程互相独立,各有自己的一份全局变量,所以worker1中修改全局变量,worker2不受影响
3- 进程之间变量独立不共享,这一点和thread完全不同
"""
import
multiprocessing
,
time
g_num
=
0
def
worker1
(
num
):
global
g_num
print
(
g_num
)
# 这里修改全局变量
g_num
+=
99
while
True
:
time
.
sleep
(
1
)
print
(
"worker1获取到的g_num=%d"
%
g_num
)
def
worker2
(
num
):
while
True
:
print
(
"worker2获取到的g_num=%d"
%
g_num
)
time
.
sleep
(
1
)
def
main
():
pro1
=
multiprocessing
.
Process
(
target
=
worker1
,
args
=
(
0
, ))
pro1
.
start
()
pro2
=
multiprocessing
.
Process
(
target
=
worker2
,
args
=
(
1
, ))
pro2
.
start
()
# if __name__ == '__main__':
# main()
"""
进程间如果想要共享全局变量的话使用中间商queue,每个进程都访问这个queue
1- queue队列,先进先出, stack栈的话,先进后出
2- 队列的存取原理是:放一个取一个, 取出队列就没有了
3- 如果队列没有东西进行get,或者put放的超过长度,都会阻塞,一个萝卜一个坑
"""
from
multiprocessing
import
queues
def
testQueue
():
# 创建一个3个长度的队列
queue
=
multiprocessing
.
Queue
(
3
)
# put放入数据
# queue.put(1)
# get读取数据
queue
.
get
()
testQueue
()
You can’t perform that action at this time.