小编use*_*020的帖子

在python中进行多处理时更改全局变量

所以我最终要做的是读取一行,用该行中的信息进行一些计算,然后将结果添加到某个全局对象,但我似乎永远无法让它工作.例如,下面的代码中的测试始终为0.我知道这是错的,我尝试过其他方式,但它仍然没有用.

import multiprocessing as mp

File = 'HGDP_FinalReport_Forward.txt'
#short_file = open(File)
test = 0

def pro(temp_line):
    global test
    temp_line = temp_line.strip().split()
    test = test + 1
    return len(temp_line)

if __name__ == "__main__":
    with open("HGDP_FinalReport_Forward.txt") as lines:
        pool = mp.Pool(processes = 10)
        t = pool.map(pro,lines.readlines())
Run Code Online (Sandbox Code Playgroud)

python multiprocessing python-2.7

5
推荐指数
1
解决办法
1万
查看次数

多处理:在某些情况下在赋值之前引用的变量,而在其他情况下不引用

我在这个网站上找到了以下示例:

import multiprocessing
import ctypes
import numpy as np

shared_array_base = multiprocessing.Array(ctypes.c_double, 10*10)
shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
shared_array = shared_array.reshape(10, 10)

# No copy was made
assert shared_array.base.base is shared_array_base.get_obj()

# Parallel processing
def my_func(i, def_param=shared_array):
    shared_array[i,:] = i

if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=4)
    pool.map(my_func, range(10))

    print shared_array
Run Code Online (Sandbox Code Playgroud)

上面的代码工作正常,但如果我想在共享数组中添加一个数组,比如shared_array + = some_other_array(而不是上面的shared_array [i,;] = i)我得到了

赋值前引用的局部变量'shared_array'

任何想法为什么我不能这样做?

python multiprocessing python-2.7

3
推荐指数
1
解决办法
1818
查看次数

多处理问题

所以我只想尝试多处理并阅读文本doc中的每一行.有660918行,我知道它们的长度相同.虽然,使用以下代码,行的长度似乎发生了变化,我无法弄清楚原因.

import multiprocessing

class Worker(multiprocessing.Process):
    def __init__(self,in_q):
        multiprocessing.Process.__init__(self)
        self.in_q = in_q
    def run(self):      
        while True:
            try:
                in_q.get()
                temp_line = short_file.readline()
                temp_line = temp_line.strip().split()
                print len(temp_line)
                self.in_q.task_done()
            except:                              
                break     

if __name__ == "__main__":
    num_proc = 10
    lines = 100000 #660918 is how many lines there actually are
    in_q = multiprocessing.JoinableQueue()
    File = 'HGDP_FinalReport_Forward.txt'
    short_file = open(File)

    for i in range(lines):
        in_q.put(i)    

    for i in range(num_proc):
        worker = Worker(in_q)
        worker.start()
    in_q.join() 
Run Code Online (Sandbox Code Playgroud)

python python-2.7

2
推荐指数
1
解决办法
150
查看次数

令人困惑的错误

在下面我得到错误

IndentationError: unindent does not match any outer indentation level (hisrel_split.py, line 25)
Run Code Online (Sandbox Code Playgroud)

elif条线.我检查了所有的缩进,甚至重新输入了大部分内容.要么我错过了一些明显的东西,要么有一些我不知道的微妙规则.有任何想法吗

from numpy import *
from pylab import *
import sys

ifp = open(sys.argv[1],"r").readlines()
data_1 = []
data_2 = []
data = []

last = int(ifp[-1].split()[0])
set_1 = range(int(round(last)/2))
set_2 = range(int(round(last)/2),(last+1))

for i in ifp:
    d = i.split()
    try:
        data.append(eval(d[2]))
    except:
        continue
    if eval(d[0]) in set_1 and eval(d[1]) in set_1:
        try:
            data_1.append(eval(d[2]))
        except:
            continue
    elif eval(d[0]) in set_2 and eval(d[1]) in set_2:
        print "yes"
Run Code Online (Sandbox Code Playgroud)

python python-2.7

2
推荐指数
1
解决办法
112
查看次数

标签 统计

python ×4

python-2.7 ×4

multiprocessing ×2