pythonsqlanywheresap-iqsqlanydb

Starting a database connection with sqlanydb inside a fork


Based on an example for forking, I build up this little script:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sqlanydb
import os

def child():
    conn = sqlanydb.connect(uid='dba', pwd='sql', eng='somedb_IQ', dbn='somedb')
    curs = conn.cursor()
    curs.execute("""SELECT * FROM foobaa;""")
    os.exit(0)

def parent():
   while True:
      newpid = os.fork()
      if newpid == 0:
         child()
      else:
         pids = (os.getpid(), newpid)
         print "parent: %d, child: %d" % pids
      if raw_input( ) == 'q': break

parent()

The intention is to do the database action inside a seperate process (big goal later is to run a huge number of queries at the same time).

But when running the script, I'm getting:

parent: 20580, child: 20587
Traceback (most recent call last):
  File "connectiontest.py", line 25, in <module>
    parent()
  File "connectiontest.py", line 19, in parent
    child()
  File "connectiontest.py", line 8, in child
    conn = sqlanydb.connect(uid='dba', pwd='sql', eng='somedb_IQ', dbn='somedb')
  File "/usr/local/lib/python2.6/dist-packages/sqlanydb.py", line 461, in connect
    return Connection(args, kwargs)
  File "/usr/local/lib/python2.6/dist-packages/sqlanydb.py", line 510, in __init__
    self.handleerror(*error)
  File "/usr/local/lib/python2.6/dist-packages/sqlanydb.py", line 520, in handleerror
    eh(self, None, errorclass, errorvalue)
  File "/usr/local/lib/python2.6/dist-packages/sqlanydb.py", line 342, in standardErrorHandler
    raise errorclass(errorvalue)
sqlanydb.OperationalError: Failed to initialize connection object

What did I might miss?


Solution

  • The issue seems to don't happen by moving import sqlanydb into child()-methode. So it would look something like:

    def child():
        import sqlanydb
    
        conn = sqlanydb.connect(uid='dba', pwd='sql', dsn='some_db')
        curs = conn.cursor()
        curs.execute("""SELECT * FROM SA100_1_1;""")
        curs.close()
        conn.close()