I have 3 procedures to execute in a Oracle DB accessed via a python script. However, instead of running them sequentially, I would like to run them in parallel. How can I achieve this?
def main():
try:
myconnection_to_a_db.cursor().execute('BEGIN MYPACKAGE1.startProcess; END;')
except Exception as e:
logging.exception(e)
try:
myconnection_to_a_db.cursor().execute('BEGIN MYPACKAGE2.startProcess; END;')
except Exception as e:
logging.exception(e)
try:
myconnection_to_a_db.cursor().execute('BEGIN MYPACKAGE3.startProcess; END;')
except Exception as e:
logging.exception(e)
Related
I want to create a connection timeout exception using urlopen.
try:
urllib2.urlopen("http://example.com", timeout = 5)
except urllib2.URLError, e:
raise MyException("There was an error: %r" % e)
This is the code
I want to create a timeout that this code would bring an exception.
Thank You in advance.
You need to catch socket.timeout exception, check example below.
import urllib2
import socket
class MyException(Exception):
pass
try:
urllib2.urlopen("http://example.com", timeout = 1)
except socket.timeout, e:
# For Python 2.7
raise MyException("There was an error: %r" % e)
I strongly recommend using Requests library for making requests, it will make your life easier.
I'm trying to catch the exception when connecting to database and have no connection, but I always freeze at pyodbc.connect(connstr). I tried all errors from documentation, tried just "except Exception" but I see my program just freeze when cannot connect to database and don't check except section. (Freeze is caused by my on-purpose disconnection from database and program just does nothing (windows "no answer") until my hard reset):
import pyodbc
connstr=('DRIVER={SQL Server Native Client 11.0};Server="server_ip";port=1433;Network Library=DBMSSOCN;Database="name";uid="uid";pwd="pwd";')
try:
print("I'm here - no problem")
conn=pyodbc.connect(connstr)
print("of course not here")
except ...no_matter_what_I_write_here... :
print("but never there too")
It was too simple (thank You Pynchia for Your suggestion):
try:
conn=pyodbc.connect(connstr, timeout=5)
except pyodbc.Error as err:
print("Couldn't connect")
Assuming you are not connecting on thread. Coz Signals will not work in non main thread.
import signal
from contextlib import contextmanager
class TimeoutError(Exception): pass
#contextmanager
def time_limit(seconds):
def signal_handler(signum, frame):
raise TimeoutError("Time out")
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(seconds)
try:
yield
finally:
signal.alarm(0)
try:
with time_limit(10):
conn=pyodbc.connect(connstr)
except TimeoutError as e:
print "couldn't connect for long time"
So I have a pretty generic logging statement after a request:
try:
r = requests.get(testUrl, timeout=10.0)
except Exception, err:
logger.error({"message": err.message})
This works great for everything I've thrown at it except TimeoutError. When the request times out the err I get back is a tuple that it tries and fails to serialize.
My question is how do I catch just this one type of error? For starters TimeoutError is not something I have access to. I have tried adding from exceptions import * but with no luck. I've also tried importing OSError because the docs say TimeoutError is a subclass, but I was unable to access TimeoutError after importing OSError.
TimeoutError docs
I plan to either list my exceptions in order:
except TimeoutError, err:
#handle this specific error
except Exception, err:
#handle all other errors
or just check for type:
except Exception, err:
if isinstance(err, TimeoutError):
#handle specific error
#handle all other errors
Python 2.7.3 & Django 1.5
You can handle requests.Timeout exception:
try:
r = requests.get(testUrl, timeout=10.0)
except requests.Timeout as err:
logger.error({"message": err.message})
except requests.RequestException as err:
# handle other errors
Example:
>>> import requests
>>> url = "http://httpbin.org/delay/2"
>>> try:
... r = requests.get(url, timeout=1)
... except requests.Timeout as err:
... print(err.message)
...
HTTPConnectionPool(host='httpbin.org', port=80): Read timed out. (read timeout=1)
I want to create socket errors (By doing things, obviously) but I've no idea how I should test if my script handles errors properly (If it dectes them.)
Currently, my code is this:
except socket.error as err:
print "Connection lost, waiting..."
time.sleep(5)
In theory, it should handle all the socket errors, print and then sleep (It's a part of a while loop.).
Any idea of how can I test it to see how it handles errors?
Use the raise statement:
try:
raise socket.error
except socket.error as err:
print "Connection lost, waiting..."
time.sleep(5)
Yet another example:
try:
raise AttributeError
except AttributeError:
print 'Sorry'
#Sorry
Also take a look at here and here
I'm using urllib.request to download files from the internet. However sometimes I get Connection Reset by Peer and I want to retry.
I tried the following, but it seems that e.errno contains socket error and not an actual errno:
while True:
try:
filename, headers = urllib.request.urlretrieve(url)
break
except IOError as e:
if e.errno != errno.ECONNRESET:
raise
except Exception as e:
raise
Any suggestions?
Well this part is not needed, first of all.
except Exception as e:
raise
And the arguments of the IOError is the type of error (socket error) and the error given to it. This error, in turn, is not the original error, but that error is in the args, so...
except IOError as e:
if e.args[1].args[0].errno != errno.ECONNRESET:
raise
Should work. I don't have a server that will reset on me, so I can't test it 100% But it works with ECONNREFUSED. :-)