我搜索过谷歌,但找不到我认为简单问题的答案.
我有一个Perl代码(下面的示例),每3秒获取一次数据,并将接收到的数据更新到MySQL数据库中,但有时MySQL数据库不可用,脚本就会死掉.如果失败,如何再次建立MySQL连接?
use DBD::Mysql;
sub updateMysqlDB{
my $connect = DBI->connect("dbi:mysql:$database:$host",
$user,
$pw,
{RaiseError => 1}
);
$myquery = "My sql query to insrt data into columns";
$query_handle=$connect->prepare($myquery);
$query_handle->execute();
$connect->disconnect;
}
while (1) {
if data received call updateMysqlDB ();
else wait for data { sleep 3 ;}
}
Run Code Online (Sandbox Code Playgroud) 我是yaml的新手,输出是预期的格式,但想知道是否有更好的方法在yaml中构造下面的嵌套对象.
import yaml
yaml.load ("""
test1:
a:
a1:
a2:
a3: 0
b3: 0
c3: 0
b2:
a3: 0
b3: 0
c3: 0
c2:
a3: 0
b3: 0
c3: 0
b1:
a2:
a3: 0
b3: 0
c3: 0
b2:
a3: 0
b3: 0
c3: 0
c2:
a3: 0
b3: 0
c3: 0
c1:
a2:
a3: 0
b3: 0
c3: 0
b2:
a3: 0
b3: 0
c3: 0
c2:
a3: 0
b3: 0
c3: 0
b:
a1:
a2:
a3: 0
b3: 0 …Run Code Online (Sandbox Code Playgroud) 从javascript jquery和使用eval我仍然无法获得jquery异步读取数据.
data1=[1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
注意:我在下面的示例中包含了async:true只是为了显示差异
下面的例子返回"null"
$(document).ready(function(){
var myArray=[];
myArray=getValues();
alert(myArray);
function getValues(){
var result=null;
$.ajax({
url: 'data1.html',
type: 'get',
dataType: 'json',
cache: false,
success: function(data) {result = data;},
async:true,
});
return result;
};
})
Run Code Online (Sandbox Code Playgroud)
以下示例工作正常,并以数组形式给出结果,即[1,2,3,4]
$(document).ready(function(){
var myArray=[];
myArray=getValues();
alert(myArray);
function getValues(){
var result=null;
$.ajax({
url: 'data1.html',
type: 'get',
dataType: 'json',
cache: false,
success: function(data) {result = data;},
async:false,
});
return result;
};
})
Run Code Online (Sandbox Code Playgroud)
有人可以解释如何异步获得结果谢谢
我有一个巨大的xlsx文件(大约127 MB),并希望使用Spreadsheet::Excel模块阅读,但我在2GB RAM机器上得到" Out of Memory"错误.(注意脚本适用于较小的excel 2007文件)
有没有办法在不达到内存限制的情况下逐行读取excel文件.搜索谷歌我遇到了http://discuss.joelonsoftware.com/default.asp?joel.3.160328.14,但我并不熟悉如何将电子表格存储到标量中.有人可以将excel 2007文件作为标量和打印单元格值进行读取.下面是我在较小的电子表格上运行的当前脚本.
#!/usr/bin/perl
use Excel::Writer::XLSX;
use Spreadsheet::XLSX;
my $workbook = Excel::Writer::XLSX->new('Book1.xlsx');
my $worksheet = $workbook->add_worksheet();
# use strict;
my $excel = Spreadsheet::XLSX -> new ('Book2.xlsx');
my $date_format = $workbook->add_format();
$date_format->set_num_format('dd/mm/yy hh:mm');
# Columns of interest
@columns=(0,1,2,5,9,10,12,13,31);
@reportlist=("string1","String2","String3");
@actuallist=("ModifiedString1","ModifiedString2","ModifiedString3");
$max_list=$#reportlist;
foreach my $sheet (@{$excel -> {Worksheet}}) {
printf("Sheet: %s\n", $sheet->{Name});
$sheet -> {MaxRow} ||= $sheet -> {MinRow};
foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) …Run Code Online (Sandbox Code Playgroud) 我知道如何处理烧瓶"在应用程序环境之外工作"的问题很少,但我无法让它为我工作
我有一个长期运行的mongo聚合查询,并计划使用apscheduler定期运行.下面是我的应用程序结构,但任务失败,出现"RuntimeError:在应用程序上下文之外工作".ihttp://flask.pocoo.org/docs/patterns/sqlite3/有一些关于使用新flask.g的例子,但想知道是否有人可以建议如何全局正确保存mongodb连接并在apscheduler中共享该连接
__init.py__
from app import create_app
Run Code Online (Sandbox Code Playgroud)
app.py
from flask import Flask, request, render_template,g
from .extention import mongo, redis, sched
def create_app(config=None):
"""Create a Flask app."""
app = Flask(__name__)
configure_extensions(app)
return app
def configure_extensions(app):
mongo.init_app(app) # initialise mongo connection from the config
redis.init_app(app)
from schedule_tasks import *
Run Code Online (Sandbox Code Playgroud)
extention.py
from flask.ext.pymongo import PyMongo
mongo = PyMongo()
from apscheduler.scheduler import Scheduler
config = {'apscheduler.jobstores.file.class': 'apscheduler.jobstores.shelve_store:ShelveJobStore',
'apscheduler.jobstores.file.path': '/tmp/sched_dbfile'}
sched = Scheduler(config)
from flask.ext.redis import Redis
redis = Redis()
Run Code Online (Sandbox Code Playgroud)
schedule_tasks.py
from .extention import mongo …Run Code Online (Sandbox Code Playgroud) 我正在使用Flask App构建器来制作基本网页。我想根据登录的用户更改默认登录页面,例如,用户1登录后应重定向到/ home / user1页面,用户2应登录到/ home / general页面等。
以下是我的自定义索引视图
class MyIndexView(IndexView):
index_template = 'index.html'
@expose('/')
def main(self):
return redirect(url_for('AuthDBView.login'))
@expose('/index')
def index(self):
return self.render_template('index.html', message="Welcome to my website")
Run Code Online (Sandbox Code Playgroud)
并通过调用启动应用
appbuilder = AppBuilder(app, db.session, indexview=MyIndexView)
Run Code Online (Sandbox Code Playgroud)
我还没有看到任何有关如何实现此目的的示例或文档。非常感谢您的帮助
如何逐行添加grep输出编号.
我有以下输出文件
pkts bytes target prot opt in out source destination
0 0 RETURN 0 -- * * 0.0.0.0/0 192.168.1.117
0 0 RETURN 0 -- * * 192.168.0.1 0.0.0.0/0
0 0 RETURN 0 -- * * 0.0.0.0/0 192.168.0.1
375993 19581223 RETURN 0 -- * * 192.168.1.136 0.0.0.0/0
752537 1043650417 RETURN 0 -- * * 0.0.0.0/0 192.168.1.136
123 9348 RETURN 0 -- * * 192.168.1.100 0.0.0.0/0
121 9196 RETURN 0 -- * * 0.0.0.0/0 192.168.1.100
Run Code Online (Sandbox Code Playgroud)
我想为每个IP地址添加所有字节并将其存储为变量以进行进一步计算.
iptables -L RRDIPT -vnx -t …Run Code Online (Sandbox Code Playgroud) 我目前正在使用jquery插件来读取数据文件(data.html)
data.html具有以下格式
[10,20,30,40,50]
Run Code Online (Sandbox Code Playgroud)
我的jquery数据请求和返回值的javascript如下
function test(){
var result=$.ajax({
url:'data.html',
type:'get',
dataType:'text',
async:false,
cache:false
}).responseText
return result;};
var my=test();
alert(my[0])
Run Code Online (Sandbox Code Playgroud)
我想以数组格式获取这些值,即我希望我的[0]值为10,但我得到"[".如果我使用eval功能
my=eval(test());
Run Code Online (Sandbox Code Playgroud)
我可以得到10,但有没有其他更好的方法将返回的ajax调用存储到数组而不是字符串?
谢谢
我尝试了下面的答案,我有点困惑,myArray中的跟随代码结果为null(在firebug中),但我把async:false然后它的工作原理.为什么我需要async:false来将值存储到数组中?(http://stackoverflow.com/questions/133310/how-can-i-get-jquery-to-perform-a-synchronous-rather-than-asynchronous-ajax-req)
jQuery.extend({getValues: function(url) {
var result = null;
$.ajax({
url: url,
type: 'get',
dataType: 'json',
cache: false,
success: function(data) {result = data;}
});
return result;}});
myArray=$.getValues("data.html");
alert(myArray[1]);
Run Code Online (Sandbox Code Playgroud)