我遇到了多处理模块的麻烦.我正在使用一个带有map方法的工作池来从大量文件加载数据,并且每个文件都使用自定义函数分析数据.每次处理文件时,我都希望更新一个计数器,以便我可以跟踪要处理的文件数量.这是示例代码:
def analyze_data( args ):
# do something
counter += 1
print counter
if __name__ == '__main__':
list_of_files = os.listdir(some_directory)
global counter
counter = 0
p = Pool()
p.map(analyze_data, list_of_files)
Run Code Online (Sandbox Code Playgroud)
我无法找到解决方案.
我的想法是创建可以求和/减去/ ...一起的特定函数对象,返回具有相同属性的新函数对象.希望这个示例代码能够证明这个想法:
from FuncObj import Func
# create some functions
quad = Func(lambda x: x**2)
cube = Func(lambda x: x**3)
# now combine functions as you like
plus = quad + cube
minus = quad - cube
other = quad * quad / cube
# and these can be called
plus(1) + minus(32) * other(5)
Run Code Online (Sandbox Code Playgroud)
我编写了以下代码,希望对此进行评论和记录,以解释我想要实现的目标.
import operator
class GenericFunction(object):
""" Base class providing arithmetic special methods.
Use derived class which must implement the
__call__ method.
"""
# this way …Run Code Online (Sandbox Code Playgroud) 我有兴趣了解以下两个函数定义之间的实际差异(如果有的话)
function foo(n::Integer)
println("hello")
end
function foo{T<:Integer}(n::T)
println("hello")
end
Run Code Online (Sandbox Code Playgroud)
据我所知,每次为新类型T调用函数时,第二种形式会触发一个新的编译,但在第一种情况下实际发生了什么?对第一种形式的相关性能有影响吗?
谢谢