首页 > 软件 > python multiprocessing 无法创建进程

python multiprocessing 无法创建进程

软件 2023-05-07

python3.6.4中用multiprocessing.Queue建立多进程不成功

可能是vs2017的问题,建议使用pycharm

你直接在python里试一下,我这里没有问题

Python Multiprocessing问题

给个示例:


#-*-coding:utf-8-*-
importthread,threading
importtime

defFuncTest(tdata):
printtdata

classmythread(threading.Thread):
def__init__(self,threadname):
threading.Thread.__init__(self)

defrun(self):
lock.acquire()
FuncTest(ft)
lock.release()

defMutiThread(num):
threads=[]
i=0
globalft
forxinxrange(num):
threads.append(mythread(num))
fortinthreads:
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()
fortinthreads:
t.join()

defGetThreadParam(datafile,num,curthread):
#线程数需要小于文件行数
f=open(datafile,'r')
lines=f.readlines()
divres=divmod(len(lines),num)
ifcurthread<(num-1):
res=lines[curthread*divres[0]:(curthread+1)*divres[0]]
elifcurthread==(num-1):
res=lines[curthread*divres[0]:((curthread+1)*divres[0]+divres[1])]
returnres
f.close()

if__name__=='__main__':

globalnum,lock
datafile='a.txt'

num=3#num并发数

lock=threading.Lock()
MutiThread(num)

a.txt文件内容如下

1

2

3

4

5

6

7

8

9

10


3个线程并发时,运行结果:

>>>

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

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

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

python Process不能多进程?

thread是一个多线程模块 如果想实现多进程,可以同时创建多个脚本同时运行

标签:信息技术 python Python入门 编程 编程语言

大明白知识网 Copyright © 2020-2022 www.wangpan131.com. Some Rights Reserved. 京ICP备11019930号-18