如何在多处理时使用用户输入

How to use user input while multiprocessing

如何存储用户输入的变量以用于函数?我正在使用多处理

我看过这篇文章,但似乎不适用于我的情况

这是我正在尝试做的事情:


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

pstbinU1 只是 returns 一个空白字符串,对于睡眠最小值和最大值,我得到 EOF 行尾错误

这是 main() 函数:

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

这里是启动块:

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

我不知道如何解决这个问题。任何帮助都会很棒...:)

如前所述,您可以使用 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