简体   繁体   English

Python-每个任务都从.txt文件读取每个名称(多处理)

[英]Python - Every tasks read each names from .txt file (Multiprocessing)

So I have been working abit with multiprocessing and finally got it to work with multiprocessing - Basically now I got it to run so every tasks runs now depending on how many tasks I enter. 因此,我一直在努力进行多处理,最后使其与多处理一起使用-基本上,现在我可以运行它,因此每个任务现在都可以运行,具体取决于我输入的任务数。

def main():


    user_input = 0
    while True:
        try:
            user_input = int(input(Fore.WHITE + 'How many tasks do you wanna run? [NUMBERS] \n' + Fore.RESET))
        except ValueError:
            print(Fore.RED + "Stop being stupid" + Fore.RESET)
            continue
        else:
            HowManyThread = user_input
            print()
            i = 0
            jobs = []
            for i in range(HowManyThread):
                p = multiprocessing.Process(target=info, args=(str(i),))
                jobs.append(p)
                time.sleep(.5)
                p.start()

            for p in jobs:
                p.join()

            sys.exit()

however I was looking through other stackoverflow threads and found 但是我正在寻找其他stackoverflow线程并发现

with open(fname) as f:
    content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip() for x in content] 

where I have all my names in fname etc: 我的所有名字都在fname等位置:

Barry Alex Sven Mila Jenny etc etc... 巴里·亚历克斯·斯文·米拉·詹妮等...

However what the multiprocessing is doing though is that it actually reads the same for each tasks and I can't really get a grip what to do now... 但是,多处理的作用实际上是每个任务读取相同的内容,而我实在无法把握现在该做什么。

Basically what I want to do is that etc... 基本上我想做的是等等...

tasks 1 to read first line tasks 2 to read second line tasks 3 to read third line etc etc... 任务1读取第一行任务2读取第二行任务3读取第三行等...

What could be the best solution for this? 最好的解决方案是什么?

You cannot do it. 你做不到。 On multiprocessing, every process has own non-shared status. 在多处理中,每个进程都有自己的非共享状态。

So every process will ask the operating system to open the file. 因此,每个进程都会要求操作系统打开文件。 Every process will start reading the file from the beginning. 每个过程将从头开始读取文件。 You can force every process to process only some lines, but every process needs to read all lines, to find where they start and where they end, to count lines. 您可以强制每个进程仅处理某些行,但是每个进程都需要读取所有行,以找到它们的开始和结束位置,并对行进行计数。

You example is not something that should be done in multiprocess, but with server (which serve lines) and every multiprocess ask the server for a new line. 您的示例不是应该在多进程中完成的事情,而是使用服务器(服务行),并且每个多进程都向服务器请求新行。 But this is much more server-client than multiprocess algorithm. 但这比多进程算法要多得多。 Or 'async' or with threads (not sure that read is atomic, so it could make things more complex.) 或“异步”或使用线程(不确定读取是原子的,因此可能会使事情变得更复杂。)

You can do this with a process which distribute lines to subprocess. 您可以使用将行分配到子流程的流程来执行此操作。 If you are on a UNIX system, you should look for os.fork() and os.pipe() . 如果您使用的是UNIX系统,则应查找os.fork()os.pipe()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM