我正在尝试在python 3x和linux/macOS中实现"记录管理器"类.这个类相对容易和直接,我想要的唯一"硬"的东西是能够在多个进程上访问相同的文件(保存结果).
从概念上讲,这似乎很简单:保存时,获取文件的独占锁.更新您的信息,保存新信息,释放文件的独占锁定.很容易.
我正在使用fcntl.lockf(file, fcntl.LOCK_EX)获得独家锁.问题是,在网上看,我发现很多不同的网站都说这不可靠,它在Windows上不起作用,对NFS的支持是不稳定的,并且macOS和macOS之间的事情可能会发生变化. Linux操作系统.
我已经接受了代码不能在Windows上运行,但我希望能够在macOS(单机)和linux(在多个NFS服务器上)上运行.
问题是我似乎无法做到这一点; 经过一段时间的调试和在macOS上传递测试后,一旦我在使用linux(ubuntu 16.04)的NFS上尝试它们,它们就失败了.问题是多个进程保存的信息之间存在不一致 - 某些进程缺少修改,这意味着锁定和保存过程出现问题.
我相信,有什么我做错了,我怀疑这可能与我在网上读到有关的问题.那么,通过NFS处理在macOS和linux上运行的同一文件的多个访问权限的正确方法是什么?
编辑
这就是将新信息写入磁盘的典型方法如下:
sf = open(self._save_file_path, 'rb+')
try:
fcntl.lockf(sf, fcntl.LOCK_EX) # acquire an exclusive lock - only one writer
self._raw_update(sf) #updates the records from file (other processes may have modified it)
self._saved_records[name] = new_info
self._raw_save() #does not check for locks (but does *not* release the lock on self._save_file_path)
finally:
sf.flush()
os.fsync(sf.fileno()) #forcing the OS to write to disk
sf.close() #release …Run Code Online (Sandbox Code Playgroud) 我正在尝试将浮点数转换为二进制表示; 我该怎么做到这一点?然而,我的目标是不受2米的限制,因此我希望能够轻松扩展到任何基础(3,4,8)ecc.
到目前为止,我对整数有一个简单的实现:
import string
LETTER = '0123456789' + string.ascii_lowercase
def convert_int(num, base):
if base == 1:
print "WARNING! ASKING FOR BASE = 1"
return '1' * num if num != 0 else '0'
if base > 36: raise ValueError('base must be >= 1 and <= 36')
num, rest = divmod(num, base)
rest = [LETTER[rest]]
while num >= base:
num, r = divmod(num, base)
rest.append(LETTER[r])
rest.reverse()
return (LETTER[num] if num else '') + ''.join(str(x) for x in rest)
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏:)
编辑: …
我有以下代码:
\nstd::unique_ptr<T> first = Get();\n\xe2\x80\xa6\nT* ptr_to_class_member = GetPtr(obj);\n*ptr_to_class_member = std::move(*first.release());\nRun Code Online (Sandbox Code Playgroud)\n这是否会按预期运行,没有副本、1 次移动并且没有内存泄漏?
\n我正在学习C++,我对内联行为感到困惑.在cppreference上,我发现"包含在多个源文件中的函数必须是内联的".他们的例子如下:
// header file
#ifndef EXAMPLE_H
#define EXAMPLE_H
// function included in multiple source files must be inline
inline int sum(int a, int b)
{
return a + b;
}
#endif
// source file #2
#include "example.h"
int a()
{
return sum(1, 2);
}
// source file #1
#include "example.h"
int b()
{
return sum(3, 4);
}
Run Code Online (Sandbox Code Playgroud)
这对我来说有点混乱 - 我认为ifndef后卫完全正在做这项工作,即在多次包含同一文件时防止出现问题.无论如何,我想测试一下,所以我准备了以下内容:
// Sum.h
#ifndef SUM_H
#define SUM_H
int sum(int a, int b);
#endif
// Sum.cpp
int sum(int a, int …Run Code Online (Sandbox Code Playgroud) 假设我有这个代码:
void MyFunc(const std::string& param) {
std::string maybe_altered_param;
if (ConditionIsMet()) {
maybe_altered_param = AlterParam(param);
} else {
maybe_altered_param = param; // <-- unnecessary copy
}
// do stuff with maybe_altered_param
// maybe_altered_param does not need to be modified further.
}
Run Code Online (Sandbox Code Playgroud)
该AlterParam函数返回字符串的副本,因此当ConditionIsMet返回 true 时,会创建一个副本以填充 Maybe_altered_param。然而,当ConditionIsMet(),这是次优的。在第二种情况下,我只想为同一个对象指定另一个名称,而不需要副本或任何类似的内容。
在这种情况下删除不必要的副本的最简单方法是什么?