Python 多处理时如何使用用户输入
如何存储用户输入的变量以用于函数?我正在使用多重处理 我看过这篇文章,但似乎不适用于我的情况 以下是我想做的:Python 多处理时如何使用用户输入,python,selenium-webdriver,python-multiprocessing,Python,Selenium Webdriver,Python Multiprocessing,如何存储用户输入的变量以用于函数?我正在使用多重处理 我看过这篇文章,但似乎不适用于我的情况 以下是我想做的: import multiprocessing from multiprocessing import Process, Manager, Value, Pool min = str(input('please input minimum seconds: ')) max = str(input('please input maximum
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只返回一个空白字符串,对于睡眠最小值和最大值,我得到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
您可以在主进程中读取输入(而不是等待
join
),并使用例如multiprocessing将读取的URL传递给子进程。JoinableQueueHi@PatrikPolakovic谢谢您的回复。我试图应用上面的相同问题,但这是一个输入的Y/N,所以我不知道如何应用它在我的情况下…我试图阅读,但我只能了解很少#10、20和23我认为是非常基本的,但我似乎不明白如何将它们应用到我的案例中谢谢你的回答&我投了赞成票,但我的分数刚刚低于15分,所以我的投票不公开。。无论如何,我试图再添加2个输入,但我再次丢失了def userProc(q,minW,maxW):pstbinU1=q.get()minW=minW.get()maxW=maxW.get()print(这里是args,{pstbinU1}:{min}:{max}')p1=multiprocessing.Process(target=userProc,args=(q,minW,maxW,)我觉得它错了。。。所以在Main上:如果url或minW或maxW!='':q、 put(text)minW.put(minW)maxW.put(maxW)
谢谢again@digilevi2006我已经编辑了解决方案。检查如何将最小值和最大值传递给子流程。您不必为此使用队列。另外,若您的程序只处理一个URL,然后退出,那个么您根本不必使用队列。您可以阅读main()
中的所有内容,然后创建子流程并将所有值作为参数传递感谢您的帮助。。。我想更新您的答案,但无法完成:(直到下次我遇到类似问题并尝试此操作-当发生特定事件时,我需要在主函数中输入一个用户输入,但有了此操作,它会在脚本开始时提示,而不是在脚本到达q.get()
时提示-这是正常的还是我做错了?(遗憾的是,无法打开新的任务)
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