python multiprocessing将仅启动第一个进程

时间:2018-08-31 12:45:44

标签: python multiprocessing

因此,我从事python方面的工作,我将从reddit下载图片,然后将其再次上传到ig,我正在尝试使下载和上传过程同时进行。我正在为此事进行多处理。

但是,只有我的第一个过程会开始...

这是“完整”代码:

def main(argv):
   timer = 15
   username = ''
   password = ''
   caption = ''
   filecontainer = ''
   isset = 0
   redisset = 0
   redname = ''
   redsort = 'new'
   redtimer = 1
   redscore = 50
   redmax = 40
   redre = False


   try:
       opts, args = getopt.getopt(argv, "hr:t:u:p:c:f:", ["reddit=", "red=", "redditname=", "freshtimer=","top","new","hot", "score=", "max=","overwrite"]) 
   except getopt.GetoptError:
       print('this.py -t <time in minutes> -u <ig-username> -p <ig-password> -c <captionContainer> -f <path to filecontainer>')
       sys.exit(2)
   for opt, arg in opts:

   ## Just some getopt stuff here...


   if isset == 4: #isset is just a way of testing if every arg was set.
       mainprozess = Process(target = runUpload(timer,username,password,caption,filecontainer))
       mainprozess.start()
       mainprozess.join()


       rdprozess = Process(target = getReddit(filecontainer,redname,redtimer,redmax,redscore,redsort,redre))
       rdprozess.start()
       rdprozess.join()
   else:
       print('you need to set all args')
       print('this.py -t <time in minutes(only INTEGER!!!)> -u <ig-username> -p <ig-password> -c <caption> -f <path to filecontainer>')
       sys.exit(1)

def runUpload(timer,username,password,caption,filecontainer):
#search for images in filecontainer


   allFiles = []

   while len(allFiles) <= 1:

       allFiles = [f for f in os.listdir(filecontainer) if os.path.isfile(os.path.join(filecontainer, f))]
       time.sleep(15)
       print("no files found...\nwaiting for files")

   r = random.randint(0,100)

   if r <= 100: #80% chance to post just an image
       postOK = postImage(allFiles,filecontainer,caption,username,password)
   #elif r >= 80: #20% chance to post an album
   #    postOK = postAlbum(allFiles,filecontainer,caption,username,password)







   if postOK == True:
       #delete old
       time.sleep(random.randint(5,45))
       #deleteOld(username,password,deleteAfter=100,staticPosts=12)



       print('\nsleeping for the next '+str(timer)+'minutes...\n')

       waittime = timer*30 + (random.randint(-60,60))

       i = 0
       while i < waittime:
           time.sleep(2)
           print(str((waittime*2-i*2)//60)+' min && '+str((waittime*2-i*2)%60)+' sec')
           i += 1

       #time.sleep(timer*60)
       runUpload(timer,username,password,caption,filecontainer)

   else:
       i = 0
       while i < 10:
           time.sleep(2)
           print('.')
           i += 1

       print("an error ocurred\nretrying")
       del allFiles[allFiles.index(randomFile)]
       try:
           os.remove(os.path.join(filecontainer,randomFile))
       except Exception as e:
           pass
       runUpload(timer,username,password,caption,filecontainer)


if __name__ == "__main__":
   freeze_support()
   main(sys.argv[1:])

但请注意这一部分:

   mainprozess = Process(target = runUpload(timer,username,password,caption,filecontainer))
   mainprozess.start()
   mainprozess.join()


   rdprozess = Process(target = getReddit(filecontainer,redname,redtimer,redmax,redscore,redsort,redre))
   rdprozess.start()
   rdprozess.join()

已添加:如果我对其进行更改,它也只会启动第一个

我不知道为什么以及如何解决... 希望您有任何建议,并祝您愉快。

更新: 妈的,我自己修好的!

不,原来我只是愚蠢的...

显然,我总是调用函数而不是在过程中放置​​目标,因为args需要放在一个额外的参数中。

解决方法是

    mainprozess = Process(target = runUpload,args=[timer,username,password,caption,filecontainer])
    rdprozess = Process(target = getReddit,args=[filecontainer,redname,redtimer,redmax,redscore,redsort,redre])

    mainprozess.start()
    rdprozess.start()
    mainprozess.join()
    rdprozess.join()

呵呵,这就是两年学习编程能使您...

对不起

2 个答案:

答案 0 :(得分:1)

将第一个join()移到代码底部。 join()的意思是“等待完成”。因此,第一个过程在开始第二个过程之前就结束了。

相反,做类似的事情

mainprozess.start()
rdprozess.start()
mainprozess.join()
rdprozess.join()

答案 1 :(得分:0)

哦,该死,我自己修好了!

不,原来我只是愚蠢的...

显然,我总是调用函数而不是在过程中放置​​目标,因为args需要放在一个额外的参数中。

解决方法是

mainprozess = Process(target = runUpload,args=[timer,username,password,caption,filecontainer])
rdprozess = Process(target = getReddit,args=[filecontainer,redname,redtimer,redmax,redscore,redsort,redre])

mainprozess.start()
rdprozess.start()
mainprozess.join()
rdprozess.join()

呵呵,这就是两年学习编程能使您...

对不起