问题描述
我有一个 table
对象,我想将它传递给多个线程。我使用 multiprocessing.Value
函数为该对象创建一个信号量。但是,它告诉我 Float32Atom
不可哈希。不知道在这种情况下该怎么做?
>>> import tables as tb
>>> f = tb.open_file('dot.h5','w')
>>> filters = tb.Filters(complevel=5,complib='blosc')
>>> n_ = 10000
>>> W_hat = f.create_carray(f.root,'data',tb.Float32Atom(),shape=(n_,n_),filters=filters)
>>> W_hat = Value(tb.Float32Atom(),W_hat)
Traceback (most recent call last):
File "<stdin>",line 1,in <module>
File "/home/lib/python3.8/multiprocessing/context.py",line 135,in Value
return Value(typecode_or_type,*args,lock=lock,File "/home/lib/python3.8/multiprocessing/sharedctypes.py",line 74,in Value
obj = RawValue(typecode_or_type,*args)
File "/home/lib/python3.8/multiprocessing/sharedctypes.py",line 48,in RawValue
type_ = typecode_to_type.get(typecode_or_type,typecode_or_type)
TypeError: unhashable type: 'Float32Atom'
解决方法
如果你只有线程(没有进程)是正确的,你可以只使用 multiprocessing.Semaphore。所有线程都在同一上下文中运行,因此您可以将其用于所有线程。
见https://docs.python.org/3/library/threading.html#semaphore-objects