Python进程中的Raw_input(Raw_input inside a Python process)

我在python中创建了一个小脚本,我想在同一时间使用多处理执行两个函数。 第一个函数将执行目录递归搜索,第二个函数将向用户显示一些问题。 虽然创建了.txt文件,但问题不会出现。 我已经看到了这个问题: 一个进程中的Python命令行输入,但作为一个初学者我不明白是什么问题以及如何解决它。 这是我的脚本:

import os import thread import time from multiprocessing import Process def writeFiles(): #open a file for writing files in it f = open("testFile.txt","w") #do the walk for root ,dirs,files in os.walk('C:\\Users'): for dir in dirs: if dir.startswith('Test'): for root ,dirs,files in os.walk('C:\\Users\\' + dir +'\Desktop'): for file in files: if file.endswith('.txt'): #include the full path f.write( os.path.join(root, file + "\n") ) #close the file f.close() def ask(): a = raw_input('Your name? ') if a == 'Tester': print 'Hello' else: print 'Bye' if __name__ == '__main__': # create processes p1 = Process( target = writeFiles) p2 = Process( target = ask) p1.start() p2.start()

I have created a small script in python where I want to execute two function on the same time using multiprocessing. The first function would do a directory recursive search and the second one will display some questions to the user. Although the .txt file is created the question doesn't appear. I have seen this question: Python command line input in a process but as a beginner I did not understand what is the problem and how to solve it. Here's my script:

import os import thread import time from multiprocessing import Process def writeFiles(): #open a file for writing files in it f = open("testFile.txt","w") #do the walk for root ,dirs,files in os.walk('C:\\Users'): for dir in dirs: if dir.startswith('Test'): for root ,dirs,files in os.walk('C:\\Users\\' + dir +'\Desktop'): for file in files: if file.endswith('.txt'): #include the full path f.write( os.path.join(root, file + "\n") ) #close the file f.close() def ask(): a = raw_input('Your name? ') if a == 'Tester': print 'Hello' else: print 'Bye' if __name__ == '__main__': # create processes p1 = Process( target = writeFiles) p2 = Process( target = ask) p1.start() p2.start()

最满意答案

最简单的方法是从主进程本身调用ask :

if __name__ == '__main__': p1 = Process(target = writeFiles) p1.start() ask()

或者你可以使用一个线程:

import threading import multiprocessing as mp import sys def ask(stdin): print 'Your name? ', a = stdin.readline().strip() if a == 'Tester': print 'Hello' else: print 'Bye' stdin.close() def writeFiles(): pass if __name__ == '__main__': p1 = mp.Process(target=writeFiles) p1.start() t1 = threading.Thread(target=ask, args=(sys.stdin,)) t1.start() p1.join() t1.join()

或者,你可以使用os.dup 作为JF Sebastian在这里显示 :

import multiprocessing as mp import sys import os def ask(stdin): print 'Your name? ', a = stdin.readline().strip() if a == 'Tester': print 'Hello' else: print 'Bye' stdin.close() def writeFiles(): pass newstdin = os.fdopen(os.dup(sys.stdin.fileno())) if __name__ == '__main__': p1 = mp.Process(target=writeFiles) p1.start() p2 = mp.Process(target=ask, args=(newstdin,)) p2.start() p1.join() p2.join()

The simplest thing to do would be to call ask from the main process itself:

if __name__ == '__main__': p1 = Process(target = writeFiles) p1.start() ask()

Or you could use a thread:

import threading import multiprocessing as mp import sys def ask(stdin): print 'Your name? ', a = stdin.readline().strip() if a == 'Tester': print 'Hello' else: print 'Bye' stdin.close() def writeFiles(): pass if __name__ == '__main__': p1 = mp.Process(target=writeFiles) p1.start() t1 = threading.Thread(target=ask, args=(sys.stdin,)) t1.start() p1.join() t1.join()

Or, you could use os.dup as J.F. Sebastian shows here:

import multiprocessing as mp import sys import os def ask(stdin): print 'Your name? ', a = stdin.readline().strip() if a == 'Tester': print 'Hello' else: print 'Bye' stdin.close() def writeFiles(): pass newstdin = os.fdopen(os.dup(sys.stdin.fileno())) if __name__ == '__main__': p1 = mp.Process(target=writeFiles) p1.start() p2 = mp.Process(target=ask, args=(newstdin,)) p2.start() p1.join() p2.join()

更多推荐