小编Jan*_*rny的帖子

Psycopg2 db连接在丢失的网络连接上挂起

问题描述

我正在使用psycopg2连接到远程主机上的PostgreSQL数据库.我打开一个连接并等待请求,然后对于每个请求,我在连接上运行查询并返回数据.

但是当连接已经打开后网络连接丢失时,下一个数据库查询会挂起,我必须手动终止该程序.

细节:

  • 它挂了至少2个小时(我等不了多久)
  • "网络故障"情况实际上是VPN下降(数据库主机只能通过VPN访问)
  • 我不能使用异步连接,因为我需要事务
  • python 2.6
  • psycopg 2.2.1
  • debian linux 6和7,64bit
  • postgresql 8.4和9.1

我想要/需要什么

我需要一些可靠的方法来在运行查询之前检测失败的连接,因此我的程序不会挂起,或者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)

我尝试了什么(以及什么不起作用):

python postgresql timeout tcp psycopg2

12
推荐指数
2
解决办法
2764
查看次数

C2061:语法错误:标识符'_TCHAR'

无法理解为什么我收到这个错误?我有点像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)

c c++

7
推荐指数
1
解决办法
9177
查看次数

除非明确请求,否则禁用某些测试的自动测试执行

我有一些非常慢的测试和许多简短的单元测试。我希望能够使用简单的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)

python unit-testing nosetests

6
推荐指数
1
解决办法
1239
查看次数

如何在show()之后为matplotlib图设置tight_layout

我的设置:

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的窗口被拉伸到一个平铺.不幸的是,这使图形变小,布局有些松散.

如果我想"收紧"它,请点击"配置子图 - >紧密布局"完成工作.但这意味着我必须每次点击图标然后点击按钮,因为我使用这个图来显示测试数据并且经常运行它,这非常烦人.所以我尝试了一些方法来使这更容易:

我试过了什么:

python linux xmonad matplotlib

6
推荐指数
1
解决办法
1073
查看次数

为什么我可以将 h264 编码视频从网络摄像头流式传输到显示器和文件,但不能流式传输原始视频?

我想从 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)

webcam gstreamer video4linux

5
推荐指数
1
解决办法
1万
查看次数

C结构中的函数定义?

我有一个我需要理解的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).有趣的是它基本上有两个构造函数,这对我来说是一个新概念.它工作正常,但我们可以编写结构的构造函数吗?我在谷歌上找不到任何东西,也许我没找对.

c constructor struct

1
推荐指数
1
解决办法
213
查看次数