日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

python 多进程 循环_python 多进程读取同一个循环处理、可以用multiprocessing

發布時間:2024/10/14 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 多进程 循环_python 多进程读取同一个循环处理、可以用multiprocessing 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

匿名用戶

1級

2014-08-15 回答

可以每個在func中加上一個參數data,data是這個線程處理的數據;

多線程處理的時候,給每個線程分配相應的data就可以了。

給個示例:

#?-*-?coding:utf-8?-*-

import?thread,threading

import?time

def?FuncTest(tdata):

print?tdata

class?mythread(threading.Thread):

def?__init__(self,threadname):

threading.Thread.__init__(self)

def?run(self):

lock.acquire()

FuncTest(ft)

lock.release()

def?MutiThread(num):

threads=[]

i=0

global?ft

for?x?in?xrange(num):

threads.append(mythread(num))

for?t?in?threads:

time.sleep(0.5)

lock.acquire()

ft=GetThreadParam(datafile,num,i)

#print?'[%s]Thread:%s,Testdata:%s'%(time.ctime(),t,ft)

i=i+1

t.start()

lock.release()

for?t?in?threads:

t.join()

def?GetThreadParam(datafile,?num,?curthread):

#線程數需要小于文件行數

f=open(datafile,'r')

lines=f.readlines()

divres=divmod(len(lines),num)

if?curthread>>

['1\n', '2\n', '3\n']

['4\n', '5\n', '6\n']

['7\n', '8\n', '9\n', '10']

總結

以上是生活随笔為你收集整理的python 多进程 循环_python 多进程读取同一个循环处理、可以用multiprocessing的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。