I using python and the module of multiprocessï¼but when I use globalï¼that have errorï¼the global name is not defined
<p>helloï¼I want to compare the distributed database  to  the postgresql</p>

<p>using <code>python</code>ï¼and using <code>multiprocessing</code> to simulate multi-role</p>

<p>I want to create the 4 random and calculate their running time</p>

<p>but when I defined the 4 random as globalï¼that prompt </p>

<blockquote>
  <p>global name âaâ is not defined</p>
</blockquote>

<p>I don't know how to solve this ï¼For every loopï¼the 4 random must have same values on distributed database and posgresqlï¼</p>

<p>here is my code</p>

<pre><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&gt;c:
        a=c
    if b&gt;d:
        b=d
    try:
        sqltest = "SELECT ogc_fid FROM testindex_1 WHERE ST_MAKEENVELOPE" + str((a,b,c,d,4326))+str("&amp;&amp; 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(
            "&amp;&amp; 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()
</code></pre>

<p>The error is from multicitus(), while making the string for sqlcitus.</p>

<p>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 <strong>same process</strong>.</p>

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

<pre><code>#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&gt;c:
        a=c
    if b&gt;d:
        b=d
    try:
        sqltest = "SELECT ogc_fid FROM testindex_1 WHERE ST_MAKEENVELOPE" + str((a,b,c,d,4326))+str("&amp;&amp; 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(
        "&amp;&amp; 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()
</code></pre>

python parallel-processing global