[英]Two independent processes are not running simultaneously
I wrote a program to analyse StackOverflow 2019 developer survey(.csv) file and used Matplotlib to present data.我编写了一个程序来分析 StackOverflow 2019 开发人员调查(.csv)文件并使用 Matplotlib 来呈现数据。
Below is my code:下面是我的代码:
import multiprocessing
from matplotlib import pyplot as plt
import re
import analyze
import deleteCache
def makepie(countries, contributors):
plt.style.use('fivethirtyeight')
plt.title(
'Top 5 Countries With Contribution IN Open Source Softwares(OSS)')
plt.pie(contributors, labels=countries, shadow=True,
startangle=90,
autopct='%2.2f%%',
wedgeprops={
'edgecolor': 'black'})
plt.tight_layout()
plt.show()
def makebar(countries, contributors):
index = range(1, 6)
plt.style.use('fivethirtyeight')
plt.title(
'Top 5 Countries With Contribution IN Open Source Softwares(OSS)')
plt.bar(index, contributors)
plt.xlabel('Countries')
plt.ylabel('Contributors')
plt.xticks(ticks=index, labels=countries)
plt.show()
def getdata():
pattern = re.compile(r'([A-Za-z ]+),(\d+)')
countries = []
contributors = []
with open(file='Top5ContributingCountries.csv', mode='r', encoding='utf-8') as f:
for line in f:
search = pattern.match(line)
countries.append(search.group(1))
contributors.append(int(search.group(2)))
print('Enter Your Choice')
print(f'1 For Pie Chart\n2 For Bar Chart\n3 For Both')
option = input()
if(option == '1'):
makepie(countries, contributors)
elif(option == '2'):
makebar(countries, contributors)
else:
makepie(countries, contributors)
makebar(countries, contributors)
if __name__ == "__main__":
analyze.analyzedata()
getdata()
deleteCache.deleteCache()
I implemented multithreading in getdata():我在 getdata() 中实现了多线程:
def getdata():
pattern = re.compile(r'([A-Za-z ]+),(\d+)')
countries = []
contributors = []
with open(file='Top5ContributingCountries.csv', mode='r', encoding='utf-8') as f:
for line in f:
search = pattern.match(line)
countries.append(search.group(1))
contributors.append(int(search.group(2)))
print('Enter Your Choice')
print(f'1 For Pie Chart\n2 For Bar Chart\n3 For Both')
option = input()
p1=multiprocessing.Process(target=makepie(countries, contributors))
p2=multiprocessing.Process(target=makebar(countries, contributors))
if(option == '1'):
p1.start()
elif(option == '2'):
p2.start()
else:
p1.start()
p2.start()
But piechart and barchart are not opening simultaneously if option='3'
.但是如果option='3'
,饼图和条形图不会同时打开。 If option='1'
or option='2'
it should only run p1
or p2
respectively but both the processes are running one after another.如果option='1'
或option='2'
它应该只分别运行p1
或p2
但两个进程都在一个接一个地运行。
What is wrong in my code?我的代码有什么问题?
target
is assigned the return value of calling makepie
and makebar
, which calls them instantly and is the equivalent to setting target=None
in this case. target
被分配了调用makepie
和makebar
的返回值,这会立即调用它们,在这种情况下相当于设置target=None
。 target
should be assigned the function and args
should be assigned the arguments: target
应分配为 function, args
应分配为 arguments:
p1=multiprocessing.Process(target=makepie,args=(countries, contributors))
p2=multiprocessing.Process(target=makebar,args=(countries, contributors))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.