为什么我的共享整数变量只有1个字节?

时间:2016-04-20 22:31:26

标签: python multiprocessing

我对Python很陌生,而且我非常多处理新手。 我正在使用Python 3.4中的多处理库,我的共享计数器变量最大值为127,然后翻到-128,这使我相信它只有1个字节的长度。为什么它不是普通int的大小?

以下是我的代码示例:

import multiprocessing
from multiprocessing import Value

def process(arg1, arg2, counter, checker):
    global attempts
    attempts = counter
    global succeeded
    succeeded = checker

    while not succeeded.value:
        if <expression>:
            succeeded.value = True
            break 
        elif <expression>:
            break
        else:
            <code that does stuff>
            with attempts.get_lock():
                attempts.value += 1
            print(attempts.value)


NUMOFPROCESSES = 5
attempts = Value('i', 0, lock=True)
succeeded = Value('b', False)

if __name__ == '__main__':
    jobs = []
    for i in range(NUMOFPROCESSES):
        p = multiprocessing.Process(target=process, args=(arg1, arg2, attempts, succeeded))
        jobs.append(p)
        p.start()

输出看起来像这样:

1
2
3
.
.
.
125
126
127
-128
-127
-126
-125

0 个答案:

没有答案