我知道Jython和IronPython可以并行运行线程(以利用Multicore或SMP机器),因为它们实现了VM.CPython中有实现或扩展吗?我不是在谈论多处理,如 http://docs.python.org/library/multiprocessing.html
与JVM或.Net中的线程相比,多处理模块中的子进程有多"重"?每个Process对象都有python运行时的开销吗?
我还看到了Tasklets的无堆栈"并行"方式,但它们使用循环调度并且不能并行运行Tasklet.
以下是从1到45范围内模拟数字彩票的必要解决方案,每次我们生成数字n1时,该数字将从可能的数字集合中删除.
是否有可能以更实用的方式实现相同目标?即使用地图,过滤等
def getNumbers :Array[Int] = {
val range = 1 to 45
var set = range.toSet
var resultSet:Set[Int] = Set()
var current: Int = 0
while(resultSet.size < 5 ){
current = Random.shuffle(set).head // pick the head of the shuffled set
set -= current
resultSet += current
}
resultSet.toArray
}
Run Code Online (Sandbox Code Playgroud)
"编辑"
示例从范围1到5中选择3个数字
Original Set is {1,2,3,4,5}
{1,2,3,4,5} shuffle(1) picked at random 3
{1,2,4,5} shuffle(2) picked at random 2
{1,4,5} shuffle(3) picked at random 4
original Set becomes {1,5}
numbers picked …Run Code Online (Sandbox Code Playgroud)