简体   繁体   English

python multiprocessing 只会启动第一个进程

[英]python multiprocessing will only start first process

So I worked on this python thing, where I would download pics from reddit and upload them again to ig and I`m trying to make the download and upload process simoultanious.所以我研究了这个 python 的东西,我会从 reddit 下载图片,然后再次上传到 ig,我试图让下载和上传过程同时进行。 I'm using multiprocessing for the matter.我正在为此使用多处理。

However, only my first process will start...但是,只有我的第一个过程会开始......

Here's the "complete" code:这是“完整”代码:

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:])

But focus on this part:但重点关注这部分:

   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()

Added: also if I change it around it will only start the first one补充:如果我改变它,它只会开始第一个

I don't know why and how to fix it... Hope you have any suggestions and have a good day.我不知道为什么以及如何解决它...希望您有任何建议并度过美好的一天。

UPDATE: Oh shit, I fixed it myself!更新:哦,该死,我自己修好了!

Nah, turns out I am just stupid...不,原来我只是愚蠢...

Apparently I always called the function instead of putting a target in the process, because the args need to be in an extra argument.显然我总是调用函数而不是在进程中放置一个目标,因为 args 需要在一个额外的参数中。

Here's the fix:这是修复:

    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()

Hah thats what two years learning programming gets you😂...哈,这就是两年学习编程的收获😂...

I'm sorry抱歉

Move the first join() to the bottom of the code.将第一个join()移到代码底部。 join() means "wait for this to finish". join()意思是“等待这个完成”。 So, the first process ends before you start the second.因此,第一个过程在您开始第二个过程之前结束。

Instead, do something like相反,做类似的事情

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

Oh shit, I fixed it myself!天哪,我自己修好了!

Nah, turns out I am just stupid...不,原来我只是愚蠢...

Apparently I always called the function instead of putting a target in the process, because the args need to be in an extra argument.显然我总是调用函数而不是在进程中放置一个目标,因为 args 需要在一个额外的参数中。

Here's the fix:这是修复:

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()

Hah thats what two years learning programming gets you😂...哈,这就是两年学习编程的收获😂...

I'm sorry抱歉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM