本文整理汇总了Python中pymysql.err.OperationalError方法的典型用法代码示例。如果您正苦于以下问题:Python err.OperationalError方法的具体用法?Python err.OperationalError怎么用?Python err.OperationalError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymysql.err
的用法示例。
在下文中一共展示了err.OperationalError方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _read_bytes
# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import OperationalError [as 别名]
def _read_bytes(self, num_bytes):
self._sock.set_readtimeout(self._read_timeout)
while True:
try:
data = self._rfile.read(num_bytes)
break
except (IOError, OSError) as e:
if e.errno == errno.EINTR:
continue
raise err.OperationalError(
2013,
"Lost connection to MySQL server during query (%s)" % (e, ))
if len(data) < num_bytes:
raise err.OperationalError(
2013, "Lost connection to MySQL server during query")
return data
示例2: retry
# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import OperationalError [as 别名]
def retry(retries=5, delay=2.0,
exc_types=(OperationalError, RequestException)):
def decorator(func):
def f_retry(*args, **kwargs):
for i in range(retries):
try:
return func(*args, **kwargs)
except exc_types as exc:
if i < retries - 1:
logger.info('%s failed (attempt %d of %d)',
func.__name__, i + 1, retries)
logger.debug('Caught exception, retrying...',
exc_info=True)
time.sleep(delay)
else:
logger.exception('Failed after %d attempts', retries)
if isinstance(exc, RequestException):
logger.debug('Response was: %r', exc.response.text)
raise
return f_retry
return decorator
示例3: test_raise_mysql_exception
# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import OperationalError [as 别名]
def test_raise_mysql_exception(self):
data = b"\xff\x15\x04Access denied"
with self.assertRaises(err.OperationalError) as cm:
err.raise_mysql_exception(data)
self.assertEqual(cm.exception.args, (1045, 'Access denied'))
示例4: test_raise_mysql_exception_client_protocol_41
# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import OperationalError [as 别名]
def test_raise_mysql_exception_client_protocol_41(self):
data = b"\xff\x15\x04#28000Access denied"
with self.assertRaises(err.OperationalError) as cm:
err.raise_mysql_exception(data)
self.assertEqual(cm.exception.args, (1045, 'Access denied'))
示例5: _write_bytes
# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import OperationalError [as 别名]
def _write_bytes(self, data):
try:
self._sock.write(data)
except (AttributeError, IOError) as e:
self._force_close()
raise err.OperationalError(
CR.CR_SERVER_GONE_ERROR,
"MySQL server has gone away (%r)" % (e,))
示例6: _wait_for_mysql_ready
# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import OperationalError [as 别名]
def _wait_for_mysql_ready(container_host, timed_out_seconds=10):
DB_NAME = 'test'
DB_USER = 'mysql'
timed_out = time.time() + timed_out_seconds
while time.time() < timed_out:
try:
return pymysql.connect(
host=container_host,
user=DB_USER,
db=DB_NAME
)
except OperationalError:
pass
raise Exception('Unable to start MySQL db {}'.format(container_host))
示例7: test_no_file
# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import OperationalError [as 别名]
def test_no_file(cursor, table_local_file):
# Test load local infile when the file does not exist
sql = "LOAD DATA LOCAL INFILE 'no_data.txt'" + \
" INTO TABLE test_load_local fields " + \
"terminated by ','"
with pytest.raises(OperationalError):
await cursor.execute(sql)
示例8: test_error_on_file_read
# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import OperationalError [as 别名]
def test_error_on_file_read(cursor, table_local_file):
with patch.object(builtins, 'open') as open_mocked:
m = MagicMock()
m.read.side_effect = OperationalError(1024, 'Error reading file')
m.close.return_value = None
open_mocked.return_value = m
with pytest.raises(OperationalError):
await cursor.execute("LOAD DATA LOCAL INFILE 'some.txt'"
" INTO TABLE test_load_local fields "
"terminated by ','")
示例9: _connect
# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import OperationalError [as 别名]
def _connect(self, sock=None):
try:
if sock is None:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = AsyncSocket(sock)
sock.set_connecttimeout(self.connect_timeout)
sock.connect((self.host, self.port))
sock.set_nodelay(True)
self._sock = sock
# self._rfile = _makefile(sock, 'rb')
self._rfile = sock
self._next_seq_id = 0
self._get_server_information()
self._request_authentication()
if self.sql_mode is not None:
c = self.cursor()
c.execute("SET sql_mode=%s", (self.sql_mode, ))
if self.init_command is not None:
c = self.cursor()
c.execute(self.init_command)
c.close()
self.commit()
if self.autocommit_mode is not None:
self.autocommit(self.autocommit_mode)
except BaseException as e:
self._rfile = None
if sock is not None:
try:
sock.close()
except:
pass
if isinstance(e, (OSError, IOError, socket.error)):
exc = err.OperationalError(
2003, "Can't connect to MySQL server on %r (%s)" % (self.host,
e))
# Keep original exception and traceback to investigate error.
exc.original_exception = e
exc.traceback = traceback.format_exc()
if DEBUG: print(exc.traceback)
raise exc
# If e is neither DatabaseError or IOError, It's a bug.
# But raising AssertionError hides original error.
# So just reraise it.
raise
示例10: connect
# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import OperationalError [as 别名]
def connect(self):
self._closed = False
self._loop = platform.current_ioloop()
try:
if self.unix_socket:
self.host_info = "Localhost via UNIX socket"
address = self.unix_socket
self._secure = True
else:
self.host_info = "socket %s:%d" % (self.host, self.port)
address = (self.host, self.port)
sock = platform.IOStream(address, self.bind_address)
sock.set_close_callback(self.stream_close_callback)
child_gr = greenlet.getcurrent()
main = child_gr.parent
assert main is not None, "Execut must be running in child greenlet"
def connected(future):
if (hasattr(future, "_exc_info") and future._exc_info is not None) \
or (hasattr(future, "_exception") and future._exception is not None):
child_gr.throw(future.exception())
else:
self._sock = sock
child_gr.switch()
future = sock.connect(address, self.connect_timeout)
future.add_done_callback(connected)
main.switch()
self._rfile = self._sock
self._next_seq_id = 0
self._get_server_information()
self._request_authentication()
if self.sql_mode is not None:
c = self.cursor()
c.execute("SET sql_mode=%s", (self.sql_mode,))
if self.init_command is not None:
c = self.cursor()
c.execute(self.init_command)
self.commit()
if self.autocommit_mode is not None:
self.autocommit(self.autocommit_mode)
except Exception as e:
if self._sock:
self._rfile = None
self._sock.close()
self._sock = None
exc = err.OperationalError(
2003, "Can't connect to MySQL server on %s (%r)" % (
self.unix_socket or ("%s:%s" % (self.host, self.port)), e))
# Keep original exception and traceback to investigate error.
exc.original_exception = e
exc.traceback = traceback.format_exc()
raise exc
示例11: _read_bytes
# 需要导入模块: from pymysql import err [as 别名]
# 或者: from pymysql.err import OperationalError [as 别名]
def _read_bytes(self, num_bytes):
if num_bytes <= self._rbuffer_size:
self._rbuffer_size -= num_bytes
return self._rbuffer.read(num_bytes)
if self._rbuffer_size > 0:
self._sock._read_buffer = self._rbuffer.read() + self._sock._read_buffer
self._sock._read_buffer_size += self._rbuffer_size
self._rbuffer_size = 0
if num_bytes <= self._sock._read_buffer_size:
data, data_len = self._sock._read_buffer, self._sock._read_buffer_size
self._sock._read_buffer = bytearray()
self._sock._read_buffer_size = 0
if data_len == num_bytes:
return bytes(data)
self._rbuffer_size = data_len - num_bytes
self._rbuffer = StringIO(data)
return self._rbuffer.read(num_bytes)
child_gr = greenlet.getcurrent()
main = child_gr.parent
assert main is not None, "Execut must be running in child greenlet"
def read_callback(future):
try:
data = future.result()
if len(data) == num_bytes:
return child_gr.switch(bytes(data))
self._rbuffer_size = len(data) - num_bytes
self._rbuffer = StringIO(data)
return child_gr.switch(self._rbuffer.read(num_bytes))
except Exception as e:
self._force_close()
return child_gr.throw(err.OperationalError(
CR.CR_SERVER_LOST,
"Lost connection to MySQL server during query (%s)" % (e,)))
try:
future = self._sock.read_bytes(num_bytes)
future.add_done_callback(read_callback)
except (AttributeError, IOError) as e:
self._force_close()
raise err.OperationalError(
CR.CR_SERVER_LOST,
"Lost connection to MySQL server during query (%s)" % (e,))
return main.switch()