user82538
user82538

Reputation: 1

I using python and the module of multiprocess,but when I use global,that have error,the global name is not defined

hello,I want to compare the distributed database to the postgresql

using python,and using multiprocessing to simulate multi-role

I want to create the 4 random and calculate their running time

but when I defined the 4 random as global,that prompt

global name ‘a’ is not defined

I don't know how to solve this (For every loop,the 4 random must have same values on distributed database and posgresql)

here is my code

#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()

Upvotes: 0

Views: 112

Answers (1)

BrightFlow
BrightFlow

Reputation: 1294

The error is from multicitus(), while making the string for sqlcitus.

I think you want to get global variables a, b, c, d declared and assigned in multitest(). But the problem is that multicitus() and sqlcitus() are in different processes, and it is impossible for them to share any global variables. Global variables can be shared among different functions in the same process.

One way to solve this problem is to use Pipes to transfer data(a, b, c, d in this example) between multicitus() and sqlcitus().

#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()

Upvotes: 1

Related Questions