私信  •  关注

Neil

Neil 最近创建的主题
Neil 最近回复了
13 年前
回复了 Neil 创建的主题 » 给我看一些很酷的python列表理解[关闭]

如果“酷”意味着疯狂,我喜欢这个:

def cointoss(n,t):
    return (lambda a:"\n".join(str(i)+":\t"+"*"*a.count(i) for i in range(min(a),max(a)+1)))([sum(randint(0,1) for _ in range(n)) for __ in range(t)])

>>> print cointoss(20,100)
3:    **
4:    ***
5:    **
6:    *****
7:    *******
8:    *********
9:    *********
10:   ********************
11:   *****************
12:   *********
13:   *****
14:   *********
15:   *
16:   **

n和t控制每次测试抛硬币的次数,以及测试运行和绘制分布的次数。

5 年前
回复了 Neil 创建的主题 » 多线程python中的同步与数据合并问题

如果一个进程是令人尴尬的并行的,并且线程之间不需要通信,那么我将只使用一个进程池上下文管理器你有很多争论,这可能很棘手,但有很多解决方法来传递争论我有时会使用嵌套函数或全局变量来转换成一个变量。否则,您总是可以发送一个列表或类似的列表。

from multiprocessing.dummy import Pool as ProcessPool # Only use this if you're CPU bound. If you're IO bound use a threadpool rather

my_zip_files = [
    ("arg1", "arg2", "arg3"),      # Whatever the arguments are. Just roll them into a tuple and store all the tuples in a list.
    ("arg12", "arg22", "arg23")
]

def do_something(arg):
    arg1 = arg[0]   # recover the individual arguments. Can use python's * syntax as well.
    arg2 = arg[1]
    arg3 = arg[2]
    result = _do_something(arg1, arg2, arg3)
    return result

def _do_something(arg1, arg2, arg3):
    <whatever logic>

with ProcessPool(4) as pool:
    result = pool.map(do_something, my_zip_files)
print(result) # And now the order of the files should be the same as the order you put them in. I've run some checks and order seems to be preserved but maybe check the docs to be sure.