简体   繁体   English

如何读取和写入numpy数组到受保护的文件

[英]How to read and write numpy arrays to protected file

I would like several processes running in parallel to read and write to the same numpy array. 我希望并行运行的多个进程读取和写入同一numpy数组。 To avoid problems, where two processes try to read/write to the same memory, I need to protect the file I am writing to. 为了避免出现问题,在两个进程试图读取/写入同一内​​存的情况下,我需要保护要写入的文件。 How do I do that? 我怎么做?

I assume that np.savetxt does not protect the file. 我认为np.savetxt不保护该文件。 I have tried the library portalocker . 我已经尝试过图书馆portalocker But by opening a file and locking it, np.savetxt is not allowed to write to the file. 但是,通过打开文件并将其锁定,则不允许np.savetxt写入文件。

See this question "Downloading over 1000 files in python" (link) for examples of using a worker thread pool. 有关使用辅助线程池的示例,请参见此问题“在python中下载超过1000个文件”(链接)

Basically you split up all of the work beforehand, put the work into a queue and let a pool of worker threads process each piece of work. 基本上,您事先将所有工作分解开来,将工作放入队列中,然后让工作线程池处理每项工作。 The workers put the results onto a another queue which can be processed by another thread to put all of the pieces together. 工作人员将结果放入另一个队列,该队列可以由另一个线程处理以将所有片段放在一起。

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

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