Morplson
Morplson

Reputation: 83

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. 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.

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

Upvotes: 1

Views: 764

Answers (2)

Morplson
Morplson

Reputation: 83

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.

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

Upvotes: 1

blue note
blue note

Reputation: 29071

Move the first join() to the bottom of the code. join() means "wait for this to finish". So, the first process ends before you start the second.

Instead, do something like

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

Upvotes: 1

Related Questions