我的python/wsgi网络应用程序中的会话有问题.每个2个wsgi守护进程中的每个线程都有一个不同的,持久的mysqldb连接.有时,在删除旧会话并创建新会话后,某些连接仍然会在select中获取旧会话,这意味着它们无法验证会话并再次请求登录.
详细信息:会话存储在本地mysql数据库的InnoDB表中.验证(通过CAS)之后,我删除该用户的任何以前的会议中,创建一个新的会话(插入行),提交事务,并重定向到最初请求的页面与Cookie中的新的会话ID.对于每个请求,将根据数据库中的会话检查cookie中的会话ID.
有时,重定向后在数据库中找不到新创建的会话.相反,该用户的旧会话仍然存在.(我通过选择并记录每个请求开头的所有会话来检查这一点).不知何故,我得到了缓存的结果.我尝试用SQL_NO_CACHE选择会话,但没有区别.
为什么我得到缓存结果?还有什么地方可以进行缓存,以及如何阻止缓存或刷新缓存?基本上,为什么其他连接无法看到新插入的数据?
我们一直在linux中开发代码,但是想编译一个windows可执行文件.旧的非gpu版本与windows中的mingw编译得很好,所以我希望我能够用CUDA版本做同样的事情.
策略是在visual studio中使用nvcc编译内核代码,其余部分在mingw中使用gcc编译.
到目前为止,我们在visual studio中轻松编译了.cu文件(内核和内核启动).但是,我们仍然无法在mingw中编译c代码.c代码包含cuda api调用,例如cudaMalloccuda类型cudaEvent_t,因此我们必须包括cuda.h和cuda_runtime.h.但是,gcc会为这些标头提供警告和错误,例如:
../include/host_defines.h:57:0: warning: "__cdecl" redefined
Run Code Online (Sandbox Code Playgroud)
和
../include/vector_functions.h:127:14: error: 'short_4' has no member named 'x'
Run Code Online (Sandbox Code Playgroud)
关于如何包含这些头文件并编译代码的c部分的任何想法?
我需要为a添加维度DataArray,在新维度上填充值.这是原始数组.
a_size = 10
a_coords = np.linspace(0, 1, a_size)
b_size = 5
b_coords = np.linspace(0, 1, b_size)
# original 1-dimensional array
x = xr.DataArray(
np.random.random(a_size),
coords=[('a', a coords)])
Run Code Online (Sandbox Code Playgroud)
我想我可以创建一个带有新维度的空DataArray并复制现有数据.
y = xr.DataArray(
np.empty((b_size, a_size),
coords=([('b', b_coords), ('a', a_coords)])
y[:] = x
Run Code Online (Sandbox Code Playgroud)
一个更好的想法可能是使用concat.我花了一段时间才弄清楚如何为concat维度指定dims和coords,并且这些选项都不是很好.有什么我想念的东西可以使这个版本更干净吗?
# specify the dimension name, then set the coordinates
y = xr.concat([x for _ in b_coords], 'b')
y['b'] = b_coords
# specify the coordinates, then rename the dimension
y = xr.concat([x for _ in …Run Code Online (Sandbox Code Playgroud) 我有一个简单的webapp来构建,我刚开始乱用mod_wsgi.在各种教程中,第一个hello world应用程序看起来如下所示:
def application(environ,start_response):
response_body = 'Hello World'
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
Run Code Online (Sandbox Code Playgroud)
然后,以后应用程序包含一个使用wsgiref的wsgi服务器,一些变体:
from wsgiref.simple_server import make_server
def application(environ, start_response):
response_body = 'Hello World'
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
httpd = make_server('localhost', 8000, application)
httpd.serve_forever()
Run Code Online (Sandbox Code Playgroud)
该应用程序无需服务器,所以服务器是什么?
实际上,给定N a(可能非常大)偶数,我想找到N = F*R,其中gcd(F,R)= 1,F> R,F尽可能小(因为我将完全保理F).问题的核心是找到最大的除数R,其中R <sqrt(N).
例如,N = 36应该给出F = 9和R = 4.请注意,R不一定是素数,也不是素数.请注意,我不考虑N.对F和R的唯一限制是它们是相对素数.
这是我的快速和天真的版本,它正在工作:
def factor_partial(N):
for R in xrange(int(math.sqrt(N)),1,-1):
if N%R == 0 and gcd(R,N/R) == 1:
return N/R, R
Run Code Online (Sandbox Code Playgroud)
我想象的另一种方法是按顺序查找除数,并沿途移除任意多个非除数.就像是:
def factor_partial(N):
i = range(2, int(sqrt(N)) + 1)
while i:
if N % i[0] != 0:
remove_multiples(i, i[0]) #without removing i[0]
else:
if gcd(i[0], N/i[0]) == 1:
R = i[0]
i.pop(0) #remove i[0]
return N/R, R
Run Code Online (Sandbox Code Playgroud)
我认为这将是缓慢和内存密集的,但也许如果i是生成器它可能是有效的.我没有太多使用发电机.
我可以改进第一个版本吗?第二个版本是否可行(我将如何做)?有一种完全不同的方法更好吗?
在python,c或pseudocode中寻找答案.
这是一个关于数论的课程.我正在实施基于Pocklington的素性测试.虽然我需要一个分解算法,但我们还没有研究过任何一个,我可能不会使用像我的班级范围之外的二次筛子.我正在寻找提出问题的具体帮助.
我有一个加权的有向图,它有大约20,000个节点.
我昨天了解了模拟滚动加权骰子的别名方法,这与做出一个选择相同(每个节点是一个加权骰子,而侧面对应于其他节点).一卷是高效的,但更新权重不是; 别名方法可能不合适,因为我将更新骰子而不是我将要滚动!
我应该使用哪种数据结构,允许频繁更新,以及哪种相应的算法最适合做出选择?
一些想法/说明:
事实是,在实践中我不需要经常做出选择(每分钟不超过一次),所以我不需要最有效的解决方案.但这是一个有趣的副项目,我有兴趣找到一个理论上最优的解决方案.
我使用time.clock和在Ubuntu上定时了一段python代码time.time:
clock elapsed time: 8.770 s
time elapsed time: 1.869 s
Run Code Online (Sandbox Code Playgroud)
我知道time.time使用系统时间和time.clock使用处理器时钟.当time.time给出比time.clock更长的经过时间时,对我来说很有意义:处理器在整个时间内都没有活动(例如,调用时间time.sleep).
但是为什么/何时处理器时钟会比系统时间大得多?
附录
我做了一个粗略的测试,使用标准映射计算相同的函数,使用进程池映射和线程池映射.可以理解,进程池速度更快,线程池更慢.更有趣的是:时钟时序小于处理器池的时间,但线程池中的时间更长.
同样,我理解为什么处理器池的时钟时序较少:假设主进程没有做太多事情,只是等待池进程完成.但是为什么线程池的时钟时间更长?任何见解?
结果:
map
time 1738.8
clock 1739.6
mp pool
time 580.1
clock 15.9
thread pool
time 3455.3
clock 5378.9
Run Code Online (Sandbox Code Playgroud)
码:
from time import clock, sleep, time
from multiprocessing.pool import ThreadPool
from multiprocessing import Pool
import random
def f(i):
x = [random.random() for j in range(100000)]
return x[i]
def t(fn):
t0, c0 = time(), …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用此SO答案中的代码.它使用Reflect.这是一份副本:
export function CustomComponent(annotation: any) {
return function (target: Function) {
var parentTarget = Object.getPrototypeOf(target.prototype).constructor;
var parentAnnotations = Reflect.getMetadata('annotations', parentTarget);
var parentAnnotation = parentAnnotations[0];
Object.keys(parentAnnotation).forEach(key => {
if (isPresent(parentAnnotation[key])) {
annotation[key] = parentAnnotation[key];
}
});
var metadata = new ComponentMetadata(annotation);
Reflect.defineMetadata('annotations', [ metadata ], target);
}
}
Run Code Online (Sandbox Code Playgroud)
首先,我遇到了这两个错误:
Property 'getMetadata' does not exist on type 'typeof Reflect'.
Property 'defineMetadata' does not exist on type 'typeof Reflect'.
Run Code Online (Sandbox Code Playgroud)
然后我跑了npm install reflect-metadata,但我不知道如何使用它.
import { Reflect } from reflect-metadata;
Module …Run Code Online (Sandbox Code Playgroud) 这个错误是什么意思?我似乎找不到任何关于它的信息。它发生在 cudaEventRecord 上。
在项目头文件中:
cudaEvent_t cudaEventStart;
Run Code Online (Sandbox Code Playgroud)
在 .c 文件中:
cudaEventCreate(&cudaEventStart);
printf("create event: %d\n", (int) cudaEventStart);
Run Code Online (Sandbox Code Playgroud)
在我的一个 .cu 文件中:
printf("record event: %d\n", (int) cudaEventStart);
cudaEventRecord(cudaEventStart);
Run Code Online (Sandbox Code Playgroud)
相关输出显示呼叫的问题是什么。由于某种原因,cudaEventStart 在我的 cu 文件中不是有效的事件资源:
create event: 44199920
record event: 0
Run Code Online (Sandbox Code Playgroud)
细节
我正在将我的代码从 Linux 移植到 Windows。它在 linux 中的同一张卡上运行良好,并且只有一些更改。我定义roundf并添加了以下内容:
typedef size_t off_t;
#define strtof(str,n) (float)strtod(str,n)
#include <float.h>
#define isnan(n) _isnan(n)
#define strcasecmp _stricmp
#include <io.h>
#define read _read
Run Code Online (Sandbox Code Playgroud)
我不清楚为什么这些事情会影响 cuda 资源。也许我以某种方式错误地构建了项目......?
SELECT * FROM a, b WHERE ...
Run Code Online (Sandbox Code Playgroud)
Mysql允许在查询结果中使用重复的列名.因此,在终端中,没有列名称使用上述查询作为前缀.
但是,我在使用DictCursor的python中使用mysqldb.结果是列表名称为键的字典列表.有时,dict游标会自动在列名前加上表名.据我所知,它是针对两个不明确的列名称中的第二个,但仅当第二个值是唯一的时才这样做.无论如何,我想强制光标使用表名为所有键添加前缀.
来自fetch.row()函数的mysqldb文档 ...
第二个参数(how)告诉它应该如何表示行.默认情况下,它为零,表示作为元组返回.how = 1表示,将其作为字典返回,其中键是列名,如果有两列具有相同的名称(例如,来自连接),则返回table.column.how = 2表示与how = 1相同,除了键总是table.column; 这是为了与旧的Mysqldb模块兼容.
所以,它似乎可行,但我没有直接使用fetch.row()函数...所以问题是,如何在获取行时使mysqldb dict游标始终使用how = 2?