我正在使用psycopg2连接到远程主机上的PostgreSQL数据库.我打开一个连接并等待请求,然后对于每个请求,我在连接上运行查询并返回数据.
但是当连接已经打开后网络连接丢失时,下一个数据库查询会挂起,我必须手动终止该程序.
细节:
我需要一些可靠的方法来在运行查询之前检测失败的连接,因此我的程序不会挂起,或者cursor.execute(..)
在失败的连接上引发异常的方法.
import psycopg2
import time
conn = psycopg2.connect("host='dbs' dbname='foo' user='joe' password='x'")
time.sleep(10) # I manually turn VPN off during this sleep..
cu = conn.cursor()
cu.execute('SELECT 1') # <- hangs here
print cu.fetchone()
cu.commit()
Run Code Online (Sandbox Code Playgroud)
设置TCP超时"全局" - 在psycopg2导入之前,我添加了:
import socket
socket.setdefaulttimeout(10)
Run Code Online (Sandbox Code Playgroud)在psycopg.connection
套接字上设置TCP超时:
..
conn = psycopg2.connect(...
s = socket.fromfd(conn.fileno(), socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
..
Run Code Online (Sandbox Code Playgroud)为psycopg.connection …
无法理解为什么我收到这个错误?我有点像c ++和微软视觉工作室的新手,有点迷失于此.感谢您的帮助.
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
#define STILL_PLAYING 1
#define QUIT 0
#define GAME_FILE "Mundo.txt";
////// the main class of the game, its a room, ull be moving around to other rooms. /////////////
struct posicao{
string posAtual;
string Descricao;
string posNorte;
string posSul;
string posLeste;
string posOeste;
};
/////////// displays the room that you are at the moment /////
void mostraposicao(posicao &posicao)
{
cout << posicao.Descricao << endl << endl;
}
////////// gets …
Run Code Online (Sandbox Code Playgroud) 我有一些非常慢的测试和许多简短的单元测试。我希望能够使用简单的nosetests
命令仅运行简短的单元测试,并且如果我决定是时候运行缓慢的测试,以便能够显式调用它们。
运行单元测试但不运行缓慢的测试
$ nosetests
Run Code Online (Sandbox Code Playgroud)
没有使用特殊的命令 - 任何出于nosetests
好奇而进入的人都会在几秒钟内感到满意。
明确请求慢速测试:
$ nosetests --somehow-magicaly-invoke-slow-tests
Run Code Online (Sandbox Code Playgroud)
没有歧义 - 我想要慢速测试(或单元测试和慢速测试 - 这并不重要)
我尝试使用nose.plugins.attrib
:
from nose.plugins.attrib import attr
import time
@attr('slow')
def test_slow_one():
time.sleep(5)
def test_unittest():
assert True
Run Code Online (Sandbox Code Playgroud)
但实际上它的作用几乎与我想要完成的目标相反 - 我必须指定额外的命令行参数才能不运行缓慢的测试。命令是:
运行单元测试,但不运行缓慢的测试
$ nosetests -v -a '!slow'
test_a.test_unittest ... ok
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
Run Code Online (Sandbox Code Playgroud)
明确请求慢速测试:
$ nosetests -v -a 'slow'
test_a.test_slow_one ... ok
----------------------------------------------------------------------
Ran 1 test in 5.005s
OK
Run Code Online (Sandbox Code Playgroud)
更糟糕的是,当有人跑步时
$ …
Run Code Online (Sandbox Code Playgroud) Debian Linux 8.3 amd64,XMonad WM,python2.7,matplotlib 1.5.1
我正在制作一个情节,例如:
import matplotlib.pyplot as plt
x = xrange(10)
y1 = [i ** 2 for i in x]
y2 = [1.0 / (i + 1) for i in x]
fig = plt.figure()
ax1 = plt.subplot(1, 2, 1)
ax1.plot(x, y1)
ax2 = plt.subplot(1, 2, 2)
ax2.plot(x, y2)
plt.show()
Run Code Online (Sandbox Code Playgroud)
因为我正在使用平铺窗口管理器,所以matplotlib的窗口被拉伸到一个平铺.不幸的是,这使图形变小,布局有些松散.
如果我想"收紧"它,请点击"配置子图 - >紧密布局"完成工作.但这意味着我必须每次点击图标然后点击按钮,因为我使用这个图来显示测试数据并且经常运行它,这非常烦人.所以我尝试了一些方法来使这更容易:
plt.tight_layout()
之前打电话show()
:
...
ax2.plot(x, y2)
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)添加按键处理程序(所以我只按"t"):
...
ax2.plot(x, y2)
def kbd_handler(event):
if event.key …
Run Code Online (Sandbox Code Playgroud)我想从 Logitech C920 网络摄像头流式传输原始视频,同时两者GStreamer 1.0 显示视频并将视频保存到文件。
如果我从相机流式传输 h264 编码的视频(相机提供硬件编码的 h264),则此方法有效,但如果我从相机流式传输原始视频,则此方法会失败。但是,如果我仅显示或仅保存到文件,则流式原始视频可以工作。
为什么它适用于 h264 视频流,但不适用于原始视频流?
h264 编码视频流从相机到显示和文件(有效):
gst-launch-1.0 -v v4l2src device=/dev/video0 \
! video/x-h264,width=640,height=480,framerate=15/1 ! tee name=t \
t. ! queue ! h264parse ! avdec_h264 ! xvimagesink sync=false \
t. ! queue ! h264parse ! matroskamux \
! filesink location='h264_dual.mkv' sync=false
Run Code Online (Sandbox Code Playgroud)
从相机到仅显示的原始视频流(有效):
gst-launch-1.0 -v v4l2src device=/dev/video0 \
! video/x-raw,format=YUY2,width=640,height=480,framerate=15/1 \
! xvimagesink sync=false
Run Code Online (Sandbox Code Playgroud)
从相机到唯一文件的原始视频流(作品):
gst-launch-1.0 -v v4l2src device=/dev/video0 \
! video/x-raw,format=YUY2,width=640,height=480,framerate=15/1 \
! videoconvert ! x264enc ! matroskamux \
! …
Run Code Online (Sandbox Code Playgroud) 我有一个我需要理解的C代码.有一个
typedef struct someStruct {
int i;
char c;
someStruct() {
i = 0;
c = 'c';
}
someStruct(char inpChar) {
i = 1;
c = inpChar;
}
} t_someStruct;
Run Code Online (Sandbox Code Playgroud)
(我知道,代码实际上没有意义或有用.我只是简化它.)所以有这个结构,它有两个成员(int i和char c).有趣的是它基本上有两个构造函数,这对我来说是一个新概念.它工作正常,但我们可以编写结构的构造函数吗?我在谷歌上找不到任何东西,也许我没找对.
python ×3
c ×2
c++ ×1
constructor ×1
gstreamer ×1
linux ×1
matplotlib ×1
nosetests ×1
postgresql ×1
psycopg2 ×1
struct ×1
tcp ×1
timeout ×1
unit-testing ×1
video4linux ×1
webcam ×1
xmonad ×1