Search code examples
pythonselenium-webdriverpython-multiprocessing

How to use user input while multiprocessing


How can I store variables from user inputs to use on functions? I'm using multiprocessing

I've seen this article but doesn't seem to apply to my situation

HERE

Here is what I'm trying to do:


    import multiprocessing
    from multiprocessing import Process, Manager, Value, Pool

    
    min = str(input('please input minimum seconds: '))
    max = str(input('please input maximum seconds: '))
    
    pstbinU1 = ''
    def userProc():
        global pstbinU1
        
        
        r = requests.get(pstbinU1)
        print(pstbinU1)
        sleep(10)
        content = r.text
        
        fp.writelines(content+'\n')
        fp.seek(0)
        links1 = fp.read()
        fp.close()
    
        
        ......codes here
   
        
        sleep(random.randint(min, max))    
       

The pstbinU1 just returns a blank string and for the sleep min and max, I get EOF end of line error

Here is the main() function:

 def main():
    
        p1 = multiprocessing.Process(target=userProc)
        p2 = multiprocessing.Process(target=devProc)
    
    
        p1.start()
        p2.start()
    
        p1.join()
        print('Please wait for the "Finished processes" message before you close the app...')
        p2.join()

And here is the starter block:

   regex5 = (r'[a-zA-Z0-9\s_-]*')   
    
    if __name__ == '__main__':
        
        start = time.perf_counter()
    
    
        while True:
            text = str(input('Enter the pastebin raw URL(in http url form -> https://pastebin.com/raw/XXXXXXXX): '))
    
    ##        matches5 = re.match(regex5, text)
    ##        if matches5:
    
            if not text == '':
                pstbinU1 = text
    
    
                main()
    
            else:
                print('Please paste a proper Pastebin Raw Link...')

I don't know how to resolve this. Any help would be awesome... :)


Solution

  • As mentioned, you can use JoinableQueue

    import multiprocessing
    from multiprocessing import Process, Manager, Value, Pool
    
    
    def userProc(q, min, max):
        
        pstbinU1 = q.get()
        print(min, max, pstbinU1)
    
        try:
            r = requests.get(pstbinU1)
            
            sleep(10)
            content = r.text
            
            fp.writelines(content+'\n')
            fp.seek(0)
            links1 = fp.read()
            fp.close()
    
            
            sleep(random.randint(min, max))    
        except Exception:
            q.task_done()
    
    def main(q, min, max):
    
        p1 = multiprocessing.Process(target=userProc, args=(q, min, max))
        
        
        p1.start()
        p1.join()
        q.join()
    
    regex5 = (r'[a-zA-Z0-9\s_-]*')   
        
    if __name__ == '__main__':
        
        min = str(input('please input minimum seconds: '))
        max = str(input('please input maximum seconds: '))
    
        q = multiprocessing.JoinableQueue()
    
        while True:
            text = str(input('Enter the pastebin raw URL(in http url form -> https://pastebin.com/raw/XXXXXXXX): '))
    
            if not text == '':
                q.put(text)
                main(q, min, max)
                break
            else:
                print('Please paste a proper Pastebin Raw Link...')
                continue