Connection pool with python - python

Why do I have this error?
from psycopg2 import pool
from logger_base import log
import sys
class Conexion:
_DATABASE = 'test_db'
_USERNAME = 'postgres'
_PASSWORD = 'Sxmhz97*/'
_HOST = '127.0.0.1'
_PORT = 5432
_MIN_CON = 1
_MAX_CON = 5
_pool = None
#classmethod
def obtenerPool(cls):
if cls._pool is None:
try:
cls._pool = pool.SimpleConnectionPool(cls._MIN_CON, cls._MAX_CON,
host=cls._HOST,
user=cls._USERNAME,
password=cls._PASSWORD,
database=cls._DATABASE,
port=cls._PORT)
return cls._pool
except Exception as e:
log.error(f'Ocurrio un error al obterner el pool: {e}')
sys.exit()
#classmethod
def obtenerConexion(cls):
conexion = cls.obtenerPool().getconn()
log.debug(f'Conexion obtenida del pool: {conexion}')
return conexion
#classmethod
def liberarConexion(cls, conexion):
cls.obtenerPool().putconn(conexion)
log.debug(f'Regresamos la conexion al pool: {conexion}')
#classmethod
def cerrarConexiones(cls):
cls.obtenerPool().closeall()
if __name__ == '__main__':
conexion1 = Conexion.obtenerConexion()
conexion2 = Conexion.obtenerConexion()
print(conexion1)
print(conexion2)
03:01:55 PM: DEBUG [Conexion.py:33] Conexion obtenida del pool: <connection object at 0x0000022F9BED29B0; dsn: 'user=postgres password=xxx dbname=test_db host=127.0.0.1 port=5432', closed: 0>
Traceback (most recent call last):
File "f:\DATOS\Python\capa_datos\Conexion.py", line 49, in <module>
conexion2 = Conexion.obtenerConexion()
File "f:\DATOS\Python\capa_datos\Conexion.py", line 32, in obtenerConexion
conexion = cls.obtenerPool().getconn()
AttributeError: 'NoneType' object has no attribute 'getconn'

Apparently cls._pool was not None in obtenerPool(cls).
So the body of the statement if cls._pool is None: was not executed, and since the only return statement of obtenerPool is inside this if statement, it returned None.

Related

How can I catch asyncio.exceptions.InvalidStateError from dns.asyncresolver.Resolver?

I'm running a load of DNS lookups through dns.asyncresolver and around 0.05% of them are throwing:
Exception in callback _SelectorDatagramTransport._read_ready()
handle: <Handle _SelectorDatagramTransport._read_ready()>
Traceback (most recent call last):
File "/usr/lib/python3.8/asyncio/events.py", line 81, in _run
self._context.run(self._callback, *self._args)
File "/usr/lib/python3.8/asyncio/selector_events.py", line 1021, in _read_ready
self._protocol.datagram_received(data, addr)
File "/usr/local/lib/python3.8/dist-packages/dns/_asyncio_backend.py", line 30, in datagram_received
self.recvfrom.set_result((data, addr))
asyncio.exceptions.InvalidStateError: invalid state
If I edit /usr/local/lib/python3.8/dist-packages/dns/_asyncio_backend.py from:
def datagram_received(self, data, addr):
if self.recvfrom:
self.recvfrom.set_result((data, addr))
self.recvfrom = None
To:
def datagram_received(self, data, addr):
if self.recvfrom:
try:
self.recvfrom.set_result((data, addr))
except Exception as e:
print(self.recvfrom)
self.recvfrom = None
I instead get:
# python3 asynctest.py
<Future cancelled>
<Future cancelled>
<Future cancelled>
Is there any way I can catch this without editing Python library files, which I'd really rather not do?
Test code below:
from dns.asyncresolver import Resolver
import asyncio
import sqlite3
async def getIPs():
conn = sqlite3.connect('hosts.db')
c = conn.cursor()
c.execute('SELECT DISTINCT job.host FROM job')
# sqlite> SELECT DISTINCT job.host FROM job LIMIT 10;
# .
# 163mx00.mxmail.netease.com.
# 163mx01.mxmail.netease.com.
# 163mx02.mxmail.netease.com.
# 163mx03.mxmail.netease.com.
# a.dailymotion.com.
# a.iana-servers.net.
# a.icann-servers.net.
# a.mx.openstreetmap.org.
# a.ns.apple.com.
# Total: 10,259
lookups = c.fetchall()
res = Resolver()
sema = asyncio.Semaphore(500)
output = [get_ips(domain[0], res, sema) for domain in lookups]
await asyncio.gather(*output, return_exceptions=False)
async def get_ips(domain, res, sema):
async with sema:
try:
ip = await res.resolve(domain, 'A')
for result in ip:
result = result.to_text()
break
except Exception as e:
pass
try:
ip = await res.resolve(domain, 'AAAA')
for result in ip:
result = result.to_text()
break
except Exception as e:
pass
asyncio.run(getIPs())

I am not able to understand why my function fails - it has to be something to do with parseUser function

This pet project is about creating a miniature ChatServer and Client. The assumptions go like this:
First the client tries to login and then validates his details which is written using tkinter. The second frame which interacts with the user is the chat widow. This consummates the client side.
It is presumed that the details of each user is stored in a sqlite db.
The server on the other hand first creates(spawns) a socket and assigns it to each user and keeps him logged in.
When the second user is logged in and tries to chat with the first the server validates the second user and socket pairs both of the users.
I have written a code here for the server to do the above activity but it fails for some reason. I get the following error:
runfile('C:/Users/CGDELL23/ChatServer/ChatServer.py', wdir='C:/Users/CGDELL23/ChatServer')
Traceback (most recent call last):
File "C:\Users\CGDELL23\ChatServer\ChatServer.py", line 18, in <module>
__main__
NameError: name '__main__' is not defined
I think it is failing in the parseUserData function - Can you please help
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 14 10:00:54 2020
#author: Sathya Devarakonda
#Module: ChatServer.py
"""
#import networkX
import socket
import sqlite3
#from sqlite3 import error
import sys
__main__
#Network Variables
cSocket,sSocket = None
#Client socket Info
(cStrBuffer,cAncData,cflags,cAddr) = None
#Boolean Variables
validUser = None
#Arrays
newUser = []
#Variables
user,password,activityType,rUser,cUser, nullUser,nullPassword,x,y = None
#DB Connection con
con = None# Connection
class ChatServer:
def spawnSocket():
sSocket.listen()
cSocket = sSocket.accept()
cSocket.connect()#Connection Established
return cSocket;
def closeSocket(cSocket):
cSocket.close
def parseUserData(cStrBuffer):
return cStrBuffer.split(',')
def disconnectUser(cUser):
DBCalls.deleteUser(cUser)
def validateUser(cSocket):
(cStrBuffer, cAncdata, cFlags, cAddr) = cSocket.recvmsg(1024)
(activityType,user,password,cUser) = parseUserData(cStrBuffer)
if (activityType == 'login'):
return(cUser)
def connectrUser(cSocket,rUser):
try:
rSocket = DBCalls.getUser(rUser)
#(rStrBuffer,rAncdata, rFlags, rAddr) = rSocket.recvmsg(1024)
socket.socketpair(cSocket,rSocket)
except socket.err as err:
print ('Creation of socket Failed')
sys.settrace()
except sqlite3.DatabaseError as error:
print ('Db query Failed')
sys.settrace()
def chatUser(cUser):
(cStrBuffer, cAncdata, cFlags, cAddr) = cSocket.recvmsg(1024)
(activityType,user,passsword,rUser) = parseUserData(cStrBuffer)
if(activityType == 'chat'):
connectrUser(cSocket,rUser)
elif (activityType == 'logoff'):
disconnectUser(rUser)
def validateLoop():
try:
#Creating a Server Socket and binding it to localhost
sSocket = socket.socket(-1,-1,-1,None)
sSocket.bind("127.0.0.1")
while(True):
cSocket = spawnSocket()
cUser = validateUser(cSocket)
if (cUser):
chatUser(cUser)
except socket.err as err:
print ('Creation of socket Failed')
sys.settrace()
class DBCalls:
def createConn():
try:
con = sqlite3.connect('SockDetails')
except socket.error as error:
print ('Creation of connection failed')
sys.settrace()
def closeConn():
try:
con = sqlite3.close()
except socket.error as error:
print ('Closing connection failed')
sys.settrace()
def insertUser(cUser,cSocket,cAddr):
try:
userCursor = con.cursor()
userCursor.execute('insert into SockDetails (cUser,cSocket,cAddr)')
con.commit
except socket.error as error:
print ('Error adding socket details')
sys.settrace()
finally:
return (True)
def checkUser(rUser):
userIn = False #Boolean Flag
try:
userCursor = con.cursor()
userCursor.execute('select * from SockDetails where user=cUser')
con.commit
except socket.error as error:
print ('Error checking socket details')
sys.settrace()
return (userIn)
def getUser(cUser):
rSocket = False
try:
userCursor = con.cursor()
userCursor.execute('select socket from SockDetails where user=cUser')
con.commit
except socket.error as error:
print ('Error adding socket details')
sys.settrace()
finally:
return (rSocket)
def deleteUser(cUser):
try:
userCursor = con.cursor()
userCursor.execute('delete * from SockDetails where user=rUser')
con.commit
except socket.error as error:
print ('Error delete socket details')
sys.settrace()
finally:
return (True)
instead of :
import sys
__main__
try to use:
import sys
if __name__ == "__main__": # in python main used like this way
"Rest of Code"

python AttributeError: 'mHID' object has no attribute 'dict'

I am trying to make a makeblock mblock implementation with python and i found this api on github but it uses python 2.7 and i am using python 3.8 so i am trying to configure the code so it can run on python 3.8 but i ran into an error which is
init mBot
bot
<lib.mBot.mSerial object at 0x02CE9898>
start with serial
<lib.mBot.mHID object at 0x02E2B310>
self.device = mHID()
'mHID' object has no attribute 'dict'
Exception in thread Thread-2:
self.start()
Traceback (most recent call last):
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 154, in __onRead
start with HID
--------------------
n = self.device.inWaiting()
<lib.mBot.mBot object at 0x002FF640>
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 96, in inWaiting
--------------------
Error in sys.excepthook:
buf = self.dict.device.read(64)
Traceback (most recent call last):
AttributeError: 'mHID' object has no attribute 'dict'
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 134, in excepthook
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\abdsak11\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.close()
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 142, in close
self.device.close()
self.run()
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 106, in close
File "C:\Users\abdsak11\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
self.dict.device.close()
AttributeError: 'mHID' object has no attribute 'dict'
Original exception was:
Traceback (most recent call last):
File "c:/Users/abdsak11/OneDrive - Lärande/Dokument/GitHub/python-for-mbot/test.py", line 15, in <module>
self._target(*self._args, **self._kwargs)
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 163, in __onRead
bot.doMove(50,50)
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 179, in doMove
self.close()
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 142, in close
self.__writePackage(bytearray([0xff,0x55,0x7,0x0,0x2,0x5]+self.short2bytes(-leftSpeed)+self.short2bytes(rightSpeed)))
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 278, in short2bytes
self.device.close()
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 106, in close
return [ord(val[0]),ord(val[1])]
TypeError: ord() expected string of length 1, but int found
self.dict.device.close()
AttributeError: 'mHID' object has no attribute 'dict'
'mHID' object has no attribute 'dict'
Exception in thread Thread-1:
Traceback (most recent call last):
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 154, in __onRead
n = self.device.inWaiting()
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 96, in inWaiting
buf = self.dict.device.read(64)
AttributeError: 'mHID' object has no attribute 'dict'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\abdsak11\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\abdsak11\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 163, in __onRead
self.close()
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 142, in close
self.device.close()
File "c:\Users\abdsak11\OneDrive - Lärande\Dokument\GitHub\python-for-mbot\lib\mBot.py", line 106, in close
self.dict.device.close()
AttributeError: 'mHID' object has no attribute 'dict'
So what i am trying to do is to control and upload code to my mbot via usb COM3 serial and thats the error that i am getting i tried everthing but its not working and i cant seem to figure out the cause of the error.
mbot.py
this is the main api that i found
import serial
import sys,time
import signal
from time import ctime,sleep
import glob,struct
from multiprocessing import Process,Manager,Array
import threading
import hid
class mSerial():
ser = None
def __init__(self):
print (self)
def start(self, port):
self.ser = serial.Serial(port,115200)
def device(self):
return self.ser
def serialPorts(self):
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
result = []
for port in ports:
s = serial.Serial()
s.port = port
s.close()
result.append(port)
return result
def writePackage(self,package):
self.ser.write(package)
sleep(0.01)
def read(self):
return self.ser.read()
def isOpen(self):
return self.ser.isOpen()
def inWaiting(self):
return self.ser.inWaiting()
def close(self):
self.ser.close()
class mHID():
def __init__(self):
print (self)
def start(self):
print ("starting start \n\n")
self.manager = Manager()
print("manager pass \n\n")
self.dict = self.manager.dict()
print("dict pass \n\n")
self.dict.device = hid.device()
print("dict device pass \n\n")
self.dict.device.open(0x0416, 0xffff)
print("dict device open pass \n\n")
self.dict.device.hid_set_nonblocking(self.device,1)
print ("start")
self.buffer = []
self.bufferIndex = 0
def enumerate(self):
print ("enumerate")
for dev in self.dict.device.enumerate():
print ('------------------------------------------------------------')
print (dev.description())
def writePackage(self,package):
buf = []
buf += [0, len(package)]
for i in range(len(package)):
buf += [package[i]]
n = self.dict.device.write(buf)
sleep(0.01)
def read(self):
c = self.buffer[0]
self.buffer = self.buffer[1:]
return chr(c)
def isOpen(self):
return True
def inWaiting(self):
buf = self.dict.device.read(64)
l = 0
if len(buf)>0:
l = buf[0]
if l>0:
for i in range(0,l):
self.buffer += [buf[i+1]]
return len(self.buffer)
def close(self):
self.dict.device.close()
class mBot():
def __init__(self):
print ("init mBot")
signal.signal(signal.SIGINT, self.exit)
self.manager = Manager()
self.__selectors = self.manager.dict()
self.buffer = []
self.bufferIndex = 0
self.isParseStart = False
self.exiting = False
self.isParseStartIndex = 0
def startWithSerial(self, port):
self.device = mSerial()
self.device.start(port)
self.start()
def startWithHID(self):
self.device = mHID()
print ("self.device = mHID()\n \n")
self.device.start()
print ("self.device.start()\n \n")
self.start()
print ("self.start()\n \n")
def excepthook(self, exctype, value, traceback):
self.close()
def start(self):
sys.excepthook = self.excepthook
th = threading.Thread(target=self.__onRead,args=(self.onParse,))
th.start()
def close(self):
self.device.close()
def exit(self, signal, frame):
self.exiting = True
sys.exit(0)
def __onRead(self,callback):
while 1:
if(self.exiting==True):
break
try:
if self.device.isOpen()==True:
n = self.device.inWaiting()
for i in range(n):
r = ord(self.device.read())
callback(r)
sleep(0.01)
else:
sleep(0.5)
except Exception as ex:
print (str(ex))
self.close()
sleep(1)
def __writePackage(self,pack):
self.device.writePackage(pack)
def doRGBLed(self,port,slot,index,red,green,blue):
self.__writePackage(bytearray([0xff,0x55,0x9,0x0,0x2,0x8,port,slot,index,red,green,blue]))
def doRGBLedOnBoard(self,index,red,green,blue):
self.doRGBLed(0x7,0x2,index,red,green,blue)
def doMotor(self,port,speed):
self.__writePackage(bytearray([0xff,0x55,0x6,0x0,0x2,0xa,port]+self.short2bytes(speed)))
def doMove(self,leftSpeed,rightSpeed):
self.__writePackage(bytearray([0xff,0x55,0x7,0x0,0x2,0x5]+self.short2bytes(-leftSpeed)+self.short2bytes(rightSpeed)))
def doServo(self,port,slot,angle):
self.__writePackage(bytearray([0xff,0x55,0x6,0x0,0x2,0xb,port,slot,angle]))
def doBuzzer(self,buzzer,time=0):
self.__writePackage(bytearray([0xff,0x55,0x7,0x0,0x2,0x22]+self.short2bytes(buzzer)+self.short2bytes(time)))
def doSevSegDisplay(self,port,display):
self.__writePackage(bytearray([0xff,0x55,0x8,0x0,0x2,0x9,port]+self.float2bytes(display)))
def doIROnBoard(self,message):
self.__writePackage(bytearray([0xff,0x55,len(message)+3,0x0,0x2,0xd,message]))
def requestLightOnBoard(self,extID,callback):
self.requestLight(extID,8,callback)
def requestLight(self,extID,port,callback):
self.__doCallback(extID,callback)
self.__writePackage(bytearray([0xff,0x55,0x4,extID,0x1,0x3,port]))
def requestButtonOnBoard(self,extID,callback):
self.__doCallback(extID,callback)
self.__writePackage(bytearray([0xff,0x55,0x4,extID,0x1,0x1f,0x7]))
def requestIROnBoard(self,extID,callback):
self.__doCallback(extID,callback)
self.__writePackage(bytearray([0xff,0x55,0x3,extID,0x1,0xd]))
def requestUltrasonicSensor(self,extID,port,callback):
self.__doCallback(extID,callback)
self.__writePackage(bytearray([0xff,0x55,0x4,extID,0x1,0x1,port]))
def requestLineFollower(self,extID,port,callback):
self.__doCallback(extID,callback)
self.__writePackage(bytearray([0xff,0x55,0x4,extID,0x1,0x11,port]))
def onParse(self, byte):
position = 0
value = 0
self.buffer+=[byte]
bufferLength = len(self.buffer)
if bufferLength >= 2:
if (self.buffer[bufferLength-1]==0x55 and self.buffer[bufferLength-2]==0xff):
self.isParseStart = True
self.isParseStartIndex = bufferLength-2
if (self.buffer[bufferLength-1]==0xa and self.buffer[bufferLength-2]==0xd and self.isParseStart==True):
self.isParseStart = False
position = self.isParseStartIndex+2
extID = self.buffer[position]
position+=1
type = self.buffer[position]
position+=1
# 1 byte 2 float 3 short 4 len+string 5 double
if type == 1:
value = self.buffer[position]
if type == 2:
value = self.readFloat(position)
if(value<-255 or value>1023):
value = 0
if type == 3:
value = self.readShort(position)
if type == 4:
value = self.readString(position)
if type == 5:
value = self.readDouble(position)
if(type<=5):
self.responseValue(extID,value)
self.buffer = []
def readFloat(self, position):
v = [self.buffer[position], self.buffer[position+1],self.buffer[position+2],self.buffer[position+3]]
return struct.unpack('<f', struct.pack('4B', *v))[0]
def readShort(self, position):
v = [self.buffer[position], self.buffer[position+1]]
return struct.unpack('<h', struct.pack('2B', *v))[0]
def readString(self, position):
l = self.buffer[position]
position+=1
s = ""
for i in Range(l):
s += self.buffer[position+i].charAt(0)
return s
def readDouble(self, position):
v = [self.buffer[position], self.buffer[position+1],self.buffer[position+2],self.buffer[position+3]]
return struct.unpack('<f', struct.pack('4B', *v))[0]
def responseValue(self, extID, value):
self.__selectors["callback_"+str(extID)](value)
def __doCallback(self, extID, callback):
self.__selectors["callback_"+str(extID)] = callback
def float2bytes(self,fval):
val = struct.pack("f",fval)
return [ord(val[0]),ord(val[1]),ord(val[2]),ord(val[3])]
def short2bytes(self,sval):
val = struct.pack("h",sval)
return [ord(val[0]),ord(val[1])]
test.py
this is the test file that i am working with
from lib.mBot import *
if __name__ == "__main__":
bot = mBot()
print ("bot \n\n")
bot.startWithSerial("COM3")
print ("start with serial\n\n")
bot.startWithHID()
print ("start with HID \n\n")
print(f'--------------------\n')
print (bot)
print(f'--------------------\n')
bot.doMove(50,50)
I believe your trouble is here:
class mHID():
def __init__(self):
print (self)
def start(self):
# ...
self.dict = self.manager.dict()
# ...
def close(self):
self.dict.device.close()
You should always define your instance attributes in __init__(), at least with an initial value of None, even if you plan to change them later.
Additionally, whenever you have a situation where "A must be called before B", you should write the code to properly handle when B is called without A. In this case, what should close() do if start() was never called? For simplicity, I'm just having it do nothing, but you may consider raising an exception.
class mHID():
def __init__(self):
print (self)
self.dct = None
def start(self):
# ...
self.dct = self.manager.dict()
# ...
def close(self):
if self.dct:
self.dct.device.close()
You'll notice I also renamed self.dict to self.dct. Although the former name may technically be allowable because of the self. namespace, dict is actually a built-in class. Under various circumstances, you could wind up dealing with shadowing. In the very least, it's confusing to always have to remind oneself that "this isn't the built-in dict, but an instance variable instead.
(Possible aside: is there a particular reason why you don't want to call start() from the initializer?)
WARNING: This is not a simple case of "copy and paste the answer". I've illustrated a couple of design principles here that you should incorporate into the rest of your code.

Auto reconnect postgresq database

I have the following code:
import psycopg2
conn = psycopg2.connect(database="****", user="postgres", password="*****", host="localhost", port="5432")
print ("Opened database successfully")
cur = conn.cursor()
cur.execute('''select * from xyz''')
print ("Table created successfully")
conn.commit()
conn.close()
Like this i have some 50 -60 complex queries, but problem is some times the postgre sql Database throws the below error
Traceback (most recent call last):
File "C:/Users/ruw2/Desktop/SQL.py", line 87, in <module>
cur.execute('''select * from xyz;''')
psycopg2.DatabaseError: server conn crashed?
server closed the connection unexpectedly.
This probably means the server terminated abnormally before or while processing the request.
Looks like the connection is lost, how can i auto connect the Postgre database
Appreciate your help
I would rely on decorators - retry and reconnect:
import psycopg2
from tenacity import retry, wait_exponential, stop_after_attempt
from typing import Callable
def reconnect(f: Callable):
def wrapper(storage: DbStorage, *args, **kwargs):
if not storage.connected():
storage.connect()
try:
return f(storage, *args, **kwargs)
except psycopg2.Error:
storage.close()
raise
return wrapper
class DbStorage:
def __init__(self, conn: string):
self._conn: string = conn
self._connection = None
def connected(self) -> bool:
return self._connection and self._connection.closed == 0
def connect(self):
self.close()
self._connection = psycopg2.connect(self._conn)
def close(self):
if self.connected():
# noinspection PyBroadException
try:
self._connection.close()
except Exception:
pass
self._connection = None
#retry(stop=stop_after_attempt(3), wait=wait_exponential())
#reconnect
def get_data(self): # pass here required params to get data from DB
# ..
cur = self._connection.cursor()
cur.execute('''select * from xyz''')
# ..
Catch the exception and reconnect:
while True:
conn = psycopg2.connect(database="****", user="postgres", password="*****", host="localhost", port="5432")
cur = conn.cursor()
try:
cur.execute('''select * from xyz''')
except psycopg2.OperationalError:
continue
break;

Python3: ReferenceError: weakly-referenced object no longer exists

Below code works perfectly in python2 with MySQLDB, how can I make it Python3 compatible?
I have debugged and searched for similar questions.
Error:
Exception ignored in: > Traceback (most recent call last): File > > > "/usr/local/lib/python3.4/dist-packages/pymysql/cursors.py", line 41, in del File > "/usr/local/lib/python3.4/dist-packages/pymysql/cursors.py", line 47, in close ReferenceError: weakly-referenced object no longer exists –
class Database():
def __init__(self):
self.host = 'localhost'
self.user = 'user'
self.password = 'pwd'
self.db = 'dbname'
self.connection = pymysql.connect(host=self.host, user=self.user, passwd=self.password, db=self.db,use_unicode=True, charset="utf8")
self.cursor = self.connection.cursor()
def storeToDB(self,ID,string,g,e):
import datetime
curr_time = datetime.datetime.now()
status = 0
try:
self.cursor.execute("""INSERT INTO master_data (`job_id`,`sstring`,`grl`,`erl`,`status`,`insert_timestamp`) \
VALUES (%s,%s,%s,%s,%s,%s)""",(jobID,search_string,g,e,status,curr_time))
self.connection.commit()
except:
self.connection.rollback()
if __name__ == "__main__":
db = Database()
db.storeToDB(20,"hello","something.com","e.com")
Try to do some opt before db operation finish :
cursor.close()
connection.close()

Categories

Resources