我使用python和多进程模块,但是当我使用global时,有错误,全局名称没有定义

时间:2016-09-22 03:31:52

标签: python parallel-processing global

你好,我想将分布式数据库与postgresql进行比较

使用python,并使用multiprocessing模拟多角色

我想创建4随机并计算他们的运行时间

但当我将4随机定义为全局时,即提示

  

全局名称'a'未定义

我不知道如何解决这个问题(对于每个循环,4随机数必须在分布式数据库和posgresql上具有相同的值)

这是我的代码

#coding=utf-8
import psycopg2
import random
import multiprocessing

conn = psycopg2.connect("dbname=test user=higis password=dbrgdbrg host=10.1.1.215 port=5432")
cur = conn.cursor()

#test-SQl operate
def multitest(num):
    global a
    global b
    global c
    global d
    a = random.randint(74,135)
    b = random.randint(18,53)
    c = random.randint(74,135)
    d = random.randint(18,53)
    if a>c:
        a=c
    if b>d:
        b=d
    try:
        sqltest = "SELECT ogc_fid FROM testindex_1 WHERE ST_MAKEENVELOPE" + str((a,b,c,d,4326))+str("&& wkb_geometry")
        cur.execute(sqltest)
        #print cur.fetchall()
    except Exception, e:
        print e


#citus-SQL operate
def multicitus(num):
    try:
        sqlcitus = "SELECT ogc_fid FROM citusindex_1 WHERE ST_MAKEENVELOPE" + str((a, b, c, d, 4326)) + str(
            "&& wkb_geometry")
        cur.execute(sqlcitus)
        #print cur.fetchall()

    except Exception,e:
        print e

#test-multi-process
if __name__ =="__main__":
    nums = 5
    for num in range(nums):
        p = multiprocessing.Process(target=multitest,args=(num,))
        #print 'process a %d is start'%num
        p.start()
        p.join()


    #citus-multi-process
    for num in range(nums):
        q = multiprocessing.Process(target=multicitus,args=(num,))
        #print 'process b %d is start'%num
        q.start()
        q.join()



cur.close()
conn.close()

1 个答案:

答案 0 :(得分:1)

错误来自multicitus(),同时为sqlcitus创建字符串。

我认为你想要在multitest()中声明和分配全局变量a,b,c,d。但问题是multicitus()和sqlcitus()处于不同的进程中,并且它们不可能共享任何全局变量。全局变量可以在相同过程中的不同功能之间共享。

解决此问题的一种方法是使用Pipes在multicitus()和sqlcitus()之间传输数据(本例中为a,b,c,d)。

#coding=utf-8
import random
import multiprocessing
from multiprocessing import Process, Pipe

#test-SQl operate
def multitest(num, conn):
    a = random.randint(74,135)
    b = random.randint(18,53)
    c = random.randint(74,135)
    d = random.randint(18,53)
    conn.send([a, b, c, d])
    if a>c:
        a=c
    if b>d:
        b=d
    try:
        sqltest = "SELECT ogc_fid FROM testindex_1 WHERE ST_MAKEENVELOPE" + str((a,b,c,d,4326))+str("&& wkb_geometry")
        print sqltest
    except Exception, e:
        print e


#citus-SQL operate
def multicitus(num, conn):
    try:
        a, b, c, d = conn.recv()
        sqlcitus = "SELECT ogc_fid FROM citusindex_1 WHERE ST_MAKEENVELOPE" + str((a, b, c, d, 4326)) + str(
        "&& wkb_geometry")
        print sqlcitus
    except Exception, e:
        print e

#test-multi-process
if __name__ =="__main__":
    nums = 5
    pips = []
    for i in range(nums):
        pips.append(Pipe())

    # connect and running a database
    for num in range(nums):
        p = multiprocessing.Process(target=multitest,args=(num, pips[num][0]))
        #print 'process a %d is start'%num
        p.start()
        p.join()

    #citus-multi-process
    for num in range(nums):
        q = multiprocessing.Process(target=multicitus,args=(num, pips[num][1]))
        #print 'process b %d is start'%num
        q.start()
        q.join()