第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

多處理不運(yùn)行函數(shù)

多處理不運(yùn)行函數(shù)

紅顏莎娜 2023-03-22 17:03:43
我有一個(gè)代碼,應(yīng)該檢查給定數(shù)字范圍內(nèi)的素?cái)?shù)。我希望它并行執(zhí)行。但是,我無(wú)法讓它運(yùn)行。根據(jù)我將數(shù)字傳遞給它的方式,它要么工作并正確檢查素?cái)?shù),但沒有并行運(yùn)行,要么它不檢查素?cái)?shù),但啟動(dòng)許多 python 進(jìn)程并且顯然并行化什么都不做。兩者顯然都不是我所希望的。我的函數(shù)is_prime()應(yīng)該檢查一個(gè)數(shù)字是否是素?cái)?shù)。在獨(dú)立模式下,它正是這樣做的。import timeimport multiprocessingdef is_prime(n):    if (n <= 1) :         return 'not a prime number'    if (n <= 3) :        return 'prime number'              if (n % 2 == 0 or n % 3 == 0) :         return 'not a prime number'        i = 5    while(i * i <= n):         if (n % i == 0 or n % (i + 2) == 0) :             return 'not a prime number'        i = i + 6        return 'prime number'該multiprocessing_func(x)函數(shù)應(yīng)該打印它得到的每個(gè)數(shù)字,如果該數(shù)字是質(zhì)數(shù),如is_prime(). 如果我定期調(diào)用該函數(shù),這也會(huì)起作用。def multiprocessing_func(x):    time.sleep(2)    print('{} is {}'.format(x, is_prime(x)))  現(xiàn)在,如果我想與多處理模塊并行運(yùn)行上述函數(shù),我嘗試將參數(shù)傳遞給上面的函數(shù),就像target=multiprocessing_func, args=(i,)下面的代碼一樣。這會(huì)導(dǎo)致啟動(dòng)許多 python 進(jìn)程,但不會(huì)檢查素?cái)?shù)。如果我改為運(yùn)行它target=multiprocessing_func(i), args=(i,),它會(huì)檢查素?cái)?shù),但不會(huì)并行檢查。if __name__ == '__main__':    starttime = time.time()    processes = []    for i in range(1,200):        p = multiprocessing.Process(target=multiprocessing_func, args=(i,))        processes.append(p)        p.start()            for process in processes:        process.join()             print('Time taken = {} seconds'.format(time.time() - starttime))有人知道我在這里做錯(cuò)了什么嗎?我真的不明白。非常感謝你的幫助!
查看完整描述

2 回答

?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊

我在 Python 2.7 中運(yùn)行它并且運(yùn)行良好。


代碼:


import multiprocessing

import time



def is_prime(n):

    if (n <= 1):

        return 'not a prime number'

    if (n <= 3):

        return 'prime number'


    if (n % 2 == 0 or n % 3 == 0):

        return 'not a prime number'


    i = 5

    while (i * i <= n):

        if (n % i == 0 or n % (i + 2) == 0):

            return 'not a prime number'

        i = i + 6


    return 'prime number'



def multiprocessing_func(x):

    time.sleep(2)

    print('{} is {}'.format(x, is_prime(x)))



if __name__ == '__main__':

    starttime = time.time()

    processes = []

    for i in range(1, 200):

        p = multiprocessing.Process(target=multiprocessing_func, args=(i,))

        processes.append(p)

        p.start()


    for process in processes:

        process.join()


    print('Time taken = {} seconds'.format(time.time() - starttime))

輸出:


3 is prime number1 is not a prime number

2 is prime number


5 is prime number6 is not a prime number


7 is prime number8 is not a prime number


9 is not a prime number

10 is not a prime number

14 is not a prime number12 is not a prime number16 is not a prime number



15 is not a prime number

13 is prime number

4 is not a prime number

17 is prime number

26 is not a prime number

21 is not a prime number24 is not a prime number


29 is prime number

20 is not a prime number27 is not a prime number22 is not a prime number19 is prime number




25 is not a prime number

18 is not a prime number

30 is not a prime number11 is prime number


23 is prime number

28 is not a prime number33 is not a prime number31 is prime number



32 is not a prime number35 is not a prime number34 is not a prime number



39 is not a prime number36 is not a prime number38 is not a prime number37 is prime number




40 is not a prime number

41 is prime number

42 is not a prime number43 is prime number


45 is not a prime number44 is not a prime number


46 is not a prime number47 is prime number48 is not a prime number



49 is not a prime number

50 is not a prime number

52 is not a prime number51 is not a prime number


53 is prime number54 is not a prime number55 is not a prime number



56 is not a prime number

59 is prime number

57 is not a prime number

61 is prime number

62 is not a prime number60 is not a prime number


64 is not a prime number63 is not a prime number58 is not a prime number



67 is prime number65 is not a prime number66 is not a prime number68 is not a prime number




69 is not a prime number

70 is not a prime number72 is not a prime number


71 is prime number

74 is not a prime number73 is prime number


75 is not a prime number

78 is not a prime number

77 is not a prime number

80 is not a prime number

83 is prime number

85 is not a prime number79 is prime number


81 is not a prime number

76 is not a prime number84 is not a prime number


82 is not a prime number

87 is not a prime number89 is prime number


88 is not a prime number

90 is not a prime number92 is not a prime number91 is not a prime number

86 is not a prime number



95 is not a prime number

93 is not a prime number94 is not a prime number


96 is not a prime number

97 is prime number99 is not a prime number98 is not a prime number



101 is prime number

103 is prime number

100 is not a prime number

105 is not a prime number

102 is not a prime number107 is prime number


104 is not a prime number

106 is not a prime number

108 is not a prime number

110 is not a prime number

109 is prime number

112 is not a prime number111 is not a prime number


113 is prime number114 is not a prime number


115 is not a prime number

116 is not a prime number

117 is not a prime number

118 is not a prime number

120 is not a prime number119 is not a prime number


121 is not a prime number

122 is not a prime number123 is not a prime number


124 is not a prime number

125 is not a prime number

126 is not a prime number

127 is prime number

128 is not a prime number

130 is not a prime number129 is not a prime number


132 is not a prime number

131 is prime number

133 is not a prime number

134 is not a prime number

135 is not a prime number

136 is not a prime number

139 is prime number

138 is not a prime number

137 is prime number

140 is not a prime number142 is not a prime number


141 is not a prime number

143 is not a prime number

145 is not a prime number

144 is not a prime number

146 is not a prime number147 is not a prime number


148 is not a prime number

149 is prime number

150 is not a prime number

151 is prime number

154 is not a prime number

152 is not a prime number153 is not a prime number


155 is not a prime number

156 is not a prime number

158 is not a prime number157 is prime number


159 is not a prime number

161 is not a prime number160 is not a prime number


162 is not a prime number

163 is prime number

164 is not a prime number

165 is not a prime number

167 is prime number166 is not a prime number


168 is not a prime number

169 is not a prime number

170 is not a prime number

171 is not a prime number

172 is not a prime number173 is prime number


174 is not a prime number

175 is not a prime number

176 is not a prime number

178 is not a prime number

179 is prime number

177 is not a prime number

181 is prime number

180 is not a prime number

183 is not a prime number182 is not a prime number


184 is not a prime number

185 is not a prime number186 is not a prime number


187 is not a prime number

188 is not a prime number

189 is not a prime number

190 is not a prime number

192 is not a prime number

191 is prime number

193 is prime number

194 is not a prime number

195 is not a prime number

196 is not a prime number197 is prime number


198 is not a prime number199 is prime number


Time taken = 2.24506902695 seconds


Process finished with exit code 0



查看完整回答
反對(duì) 回復(fù) 2023-03-22
?
qq_花開花謝_0

TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個(gè)贊

你的代碼是正確的。

創(chuàng)建一個(gè)進(jìn)程是一項(xiàng)繁重的任務(wù),你的功能非???,所以你可能會(huì)得到一個(gè)進(jìn)程并快速運(yùn)行,它退出,然后再準(zhǔn)備和啟動(dòng)下一個(gè)進(jìn)程。

注意:從你的邏輯來(lái)看is_prime(),整數(shù)越小,函數(shù)執(zhí)行時(shí)間越短。因此,在這種情況下,查看流程也無(wú)濟(jì)于事。

我修改了你的代碼:

time.sleep(random.randint(2, 6))

所以現(xiàn)在你在每個(gè)過程中都有更多的時(shí)間(和隨機(jī)的)。

但是您可能必須為每個(gè)進(jìn)程計(jì)算更大的塊,以便為進(jìn)程提供一些工作(并將它們放入內(nèi)核調(diào)度程序)(例如,檢查 10_000 個(gè)素?cái)?shù),主程序只發(fā)送塊的開始),或者檢查更大的multiprocessing_func數(shù)字.

所以:你的代碼是正確的,但 CPU 沒有真正的工作,以便真正完整地看到多處理。


查看完整回答
反對(duì) 回復(fù) 2023-03-22
  • 2 回答
  • 0 關(guān)注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)