如果从不同进程/线程同时访问文件会发生什么?我知道没有标准的方法来锁定文件,只有os特定的功能.
在我的情况下,文件将经常被阅读并且很少被写入.现在,如果A打开一个文件进行阅读(ifstream)并开始阅读块.并B打开相同的文件进行写入(ofstream)并开始编写.会发生什么?有定义的行为吗?
编辑 我的目标是对许多文件进行并发读取,写入访问.但写访问不会经常发生.如果fstream保证文件内容不会混淆,我会很满意.
例如:进程1和进程2写入文件A.如果它们同时写入,我不在乎是否将1或2的版本写入光盘,只要它是文件的一致版本即可.
如果进程读取文件而另一个进程同时写入该文件,我希望读取过程获得该文件的"旧"版本.
如果fstreams没有处理这个,我将使用数据库.
我过去常常使用带有mod_python的cherrypy,我用cherrypy.tree.mount调用构建我的控制器树,我想保留它们(它们通过代码传播).现在我必须使用mod_wsgi.示例:来自cherrypy wiki
import sys
sys.stdout = sys.stderr
import atexit
import threading
import cherrypy
cherrypy.config.update({'environment': 'embedded'})
if cherrypy.__version__.startswith('3.0') and cherrypy.engine.state == 0:
cherrypy.engine.start(blocking=False)
atexit.register(cherrypy.engine.stop)
class Root(object):
def index(self):
return 'Hello World!'
index.exposed = True
application = cherrypy.Application(Root(), script_name=None, config=None)
Run Code Online (Sandbox Code Playgroud)
我的问题是每次cherrypy.tree.mount通话都会产生一个cherrypy.Application.mod_wsgi想要一个名为的对象'application'.
我知道你可以用类变量构建一个樱桃树,但我不想这样做.
有没有办法使用cherrypy.tree.mount并获得一个应用程序对象?
还有cherrypy.tree.graft,但我认为这是出于不同的目的.
谁可以给我解释一下这个:
char* a;
unsigned char* b;
b = a;
// error: invalid conversion from ‘char*’ to ‘unsigned char*’
b = static_cast<unsigned char*>(a);
// error: invalid static_cast from type ‘char*’ to type ‘unsigned char*’
b = static_cast<unsigned char*>(static_cast<void*>(a));
// everything is fine
Run Code Online (Sandbox Code Playgroud)
2和3之间的区别是什么?如果将3的方法用于其他(更复杂的)类型,是否存在任何陷阱?
[编辑]有些人提到糟糕的设计等...
这个简单的例子来自一个图像库,它给出了指向图像数据的指针char*.显然,图像强度始终是正的,因此我需要将其解释为unsigned char数据.
我想从对象的角度来描述python代码.例如:
foo = Foo()
profiled_foo = add_profiling(foo)
# use profiled_foo like foo
...
# later
profiled_foo.print_profile()
Run Code Online (Sandbox Code Playgroud)
我希望按方法调用每个方法花费的累计时间.我没有发现任何类似的东西,虽然我认为写起来不应该太难.
这样的图书馆存在吗?或者也许不是因为这种方式分析会是一个坏主意?
根据Paul McGuire的回答:
import inspect
from time import sleep
from profilehooks import profile
class Foo(object):
def a(self):
sleep(0.1)
def b(self):
sleep(0.3)
def c(self):
sleep(0.5)
def add_profiling(obj):
for k in dir(obj):
attr = getattr(obj, k)
if inspect.ismethod(attr) and k != '__init__':
setattr(obj, k, profile(attr))
if __name__ == '__main__':
foo = Foo()
add_profiling(foo)
foo.a()
foo.a()
foo.b()
foo.b()
foo.a()
foo.c()
Run Code Online (Sandbox Code Playgroud)
.
*** PROFILER RESULTS ***
c …Run Code Online (Sandbox Code Playgroud) 我写了一个小的分析类,附带一个防护类,使用如下:
{
ProfileGuard pg("SampleName");
// Code to profile
...
}
Run Code Online (Sandbox Code Playgroud)
但后来我注意到gcc有时会优化代码,以便pg立即被破坏.显然它会检测到,pg其他代码不会影响彼此.阻止gcc这样做的最佳方法是什么?
更新: 由于对我的问题有一些严重的怀疑,我会再次检查我的代码.我的问题基于:
3.7.3 3.
如果具有自动存储持续时间的变量具有初始化或具有副作用的析构函数,则不应在其块结束之前销毁它,也不应将其作为优化消除,即使它看起来是未使用的,除了类对象或其复制/移动可以按照12.8的规定予以删除.
打印是否有"副作用"?
请考虑以下代码.它没问题还是会导致未定义的行为?
#include <iostream>
int main()
{
{
unsigned char binary[] = {0, 5, 10};
bool* x = reinterpret_cast<bool*>(&binary[0]);
for (unsigned int i = 0; i < 3; ++i)
{
std::cout << (x[i] ? 1 : 0) << " ";
}
}
{
unsigned char b = 255;
bool* x = reinterpret_cast<bool*>(&b);
std::cout << (*x ? 1 : 0) << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用gcc 4.6到4.8编译时的输出是
0 5 10 1
但只有优化(-O1和更多).Clang导致
0 1 1 1
即使有优化.现在,如果更改y[i] ? …