下面的代码会导致Nuke挂起。基本上,我要做的是从文件系统中获取文件和文件夹的列表,并试图通过并行处理来加快它的速度。这完全可以在Nuke之外工作,但正如我前面说过的,在Nuke中运行这个会导致Nuke挂起。有什么更好的方法可以让努克不被绞死吗?最好,我希望通过Python的标准库或平台无关的包来修复这个问题。但是,如果没办法这么做的话,那我也没意见。最坏的情况下,我将不得不回到不使用并行处理和找到其他优化。
此外,当我在Nuke中运行这段代码时,控制台中会出现以下错误:
Unknown units in -c from multiprocessing.forking import main; main()守则:
#!/bin/env python
import multiprocessing
import os
CPU_COUNT = multiprocessing.cpu_count()
def _threaded_master(root):
in_queue = multiprocessing.JoinableQueue()
folder_queue = multiprocessing.JoinableQueue()
file_queue = multiprocessing.JoinableQueue()
in_queue.put(root)
for _ in xrange(CPU_COUNT):
multiprocessing.Process(target=_threaded_slave, args=(in_queue, folder_queue, file_queue)).start()
in_queue.join()
return {"folders": folder_queue, "files": file_queue}
def _threaded_slave(in_queue, folder_queue, file_queue):
while True:
path_item = in_queue.get()
if os.path.isdir(path_item):
for item in os.listdir(path_item):
path = os.path.join(path_item, item)
in_queue.put(path)
in_queue.task_done()
if __name__ == "__main__":
print _threaded_master(r"/path/to/root")发布于 2014-08-27 19:49:09
下面是我使用几个线程扫描目录树的代码。
我最初编写代码是为了使用好的旧multiprocessing.Pool(),因为它非常简单,并给出了函数的结果。不需要输入和输出队列。另一个不同之处在于它使用进程而不是线程,而线程有一些权衡。
Pool有一个很大的缺点:它假设您有一个要处理的静态项列表。
因此,我按照您最初的示例重写了代码:要处理的目录的输入/输出队列,以及输出队列。调用方必须显式地从输出队列中获取项。
对于笑容,我做了一个时间比较,好的老os.walk()和..。至少在我的机器上,传统的解决方案更快。这两种解决方案产生了数量迥异的文件,我无法解释。
玩得开心!
来源
#!/bin/env python
import multiprocessing, threading, time
import logging, os, Queue, sys
logging.basicConfig(
level=logging.INFO,
format="%(asctime)-4s %(levelname)s %(threadName)s %(message)s",
datefmt="%H:%M:%S",
stream=sys.stderr,
)
def scan_dir(topdir):
try:
for name in os.listdir(topdir):
path = os.path.join(topdir, name)
yield (path, os.path.isdir(path))
except OSError:
logging.error('uhoh: %s', topdir)
def scan_dir_queue(inqueue, outqueue):
logging.info('start')
while True:
try:
dir_item = inqueue.get_nowait()
except Queue.Empty:
break
res = list( scan_dir(dir_item) )
logging.debug('- %d paths', len(res))
for path,isdir in res:
outqueue.put( (path,isdir) )
if isdir:
inqueue.put(path)
logging.info('done')
def thread_master(root):
dir_queue = Queue.Queue() # pylint: disable=E1101
dir_queue.put(root)
result_queue = Queue.Queue()
threads = [
threading.Thread(
target=scan_dir_queue, args=[dir_queue, result_queue]
)
for _ in range(multiprocessing.cpu_count())
]
for th in threads:
th.start()
for th in threads:
th.join()
return result_queue.queue
if __name__ == "__main__":
topdir = os.path.expanduser('~')
start = time.time()
res = thread_master(topdir)
print 'threaded:', time.time() - start
print len(res), 'paths'
def mywalk(topdir):
for (dirpath, _dirnames, filenames) in os.walk(topdir):
for name in filenames:
yield os.path.join(dirpath, name)
start = time.time()
res = list(mywalk(topdir))
print 'os.walk:', time.time() - start
print len(res), 'paths'输出
11:56:35 INFO Thread-1 start
11:56:35 INFO Thread-2 start
11:56:35 INFO Thread-3 start
11:56:35 INFO Thread-4 start
11:56:35 INFO Thread-2 done
11:56:35 INFO Thread-3 done
11:56:35 INFO Thread-4 done
11:56:42 INFO Thread-1 done
threaded: 6.49218010902
299230 paths
os.walk: 1.6940600872
175741 paths发布于 2019-06-04 01:20:15
下面是一个可以参考的链接:https://learn.foundry.com/nuke/developers/63/pythondevguide/threading.html
值得注意的是其中提到的警告:nuke.executeInMainThread和nuke.executeInMainThreadWithResult应该始终从子线程运行。如果从主线程内部运行,它们就会冻结核弹。
所以,产生一个新的子线程,并在那里做你的东西。
https://stackoverflow.com/questions/25532979
复制相似问题