Multi threaded file processing with .NET

后端 未结 6 533
栀梦
栀梦 2021-01-30 15:21

There is a folder that contains 1000s of small text files. I aim to parse and process all of them while more files are being populated into the folder. My intention is to multit

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 15:27

    You might consider a queue of files to process. Populate the queue once by scanning the directory when you start and have the queue updated with a FileSystemWatcher to efficiently add new files to the queue without constantly re-scanning the directory.

    If at all possible, read and write to different physical disks. That will give you maximum IO performance.

    If you have an initial burst of many files to process and then an uneven pace of new files being added and this all happens on the same disk (read/write), you could consider buffering the processed files to memory until one of two conditions applies:

    • There are (temporarily) no new files
    • You have buffered so many files that you don't want to use more memory for buffering (ideally a configurable threshold)

    If your actual processing of the files is CPU intensive, you could consider having one processing thread per CPU core. However, for "normal" processing CPU time will be trivial compared to IO time and the complexity would not be worth any minor gains.

提交回复
热议问题