多个进程可以写入同一个文件夹吗?(Can multiple processes write to the same folder?)

我在Ubuntu机器上运行Python中的几个进程(使用multiprocessing.Process)。 每个进程都写入各种临时文件。 每个进程都写入不同的文件,但所有文件都在同一个文件夹中。 这里有潜在的错误风险吗?

我认为可能存在问题的原因是,AFAIK,Unix中的文件夹只是一个文件。 所以它就像几个进程同时写入同一个文件一样,这可能会导致信息丢失。 这真的是潜在的风险吗? 如果是这样,如何解决?


I run several processes in Python (using multiprocessing.Process) on an Ubuntu machine. Each of the processes writes various temporary files. Each process writes different files, but all files are in the same folder. Is there any potential risk of error here?

The reason I think there might be a problem is that, AFAIK, a folder in Unix is just a file. So it's jsut like several processes writing to the same file at the same time, which might cause a loss of information. Is this really a potential risk here? If so, how to solve it?


原文:https://stackoverflow.com/questions/48191238
2022-04-25 16:04

满意答案

如果你可以将枚举提升到编译时常数,那么它是可能的:

template <MessageType E, class Data>
void Send(Data const& ) { ... }

我们可以创建一个类模板,专门针对枚举所期望的每个枚举:

template <MessageType E> struct expected_type;
template <> struct expected_type<MessageType::TypeA> { using type = Foo; };
template <> struct expected_type<MessageType::TypeB> { using type = Bar; };
template <> struct expected_type<MessageType::TypeM> { using type = Foo; };

template <MessageType E>
using expected_type_t = typename expected_type<E>::type;

然后我们可以使用它来编写静态断言:

template <MessageType E, class Data>
void Send(Data const& ) {
    static_assert(std::is_same<Data, expected_type_t<E>>{}, "!");
    // ...
}

或者,可以使用该类模板直接设置Data类型:

template <MessageType E>
void Send(expected_type_t<E> const& ) {
    ...
}

If you can lift the enum to a compile time constant, then it's possible:

template <MessageType E, class Data>
void Send(Data const& ) { ... }

We can create a class template, specialized on each enum with what that enum expects:

template <MessageType E> struct expected_type;
template <> struct expected_type<MessageType::TypeA> { using type = Foo; };
template <> struct expected_type<MessageType::TypeB> { using type = Bar; };
template <> struct expected_type<MessageType::TypeM> { using type = Foo; };

template <MessageType E>
using expected_type_t = typename expected_type<E>::type;

And then we can use that to just write that static assert:

template <MessageType E, class Data>
void Send(Data const& ) {
    static_assert(std::is_same<Data, expected_type_t<E>>{}, "!");
    // ...
}

Alternatively, could use that class template to set the Data type directly:

template <MessageType E>
void Send(expected_type_t<E> const& ) {
    ...
}

相关问答

更多

当模板参数相同时,C ++优化类模板函数(C++ optimise class template function when template parameters identical)

因此,关于模板化类的模板成员的显式特化,有一些奇怪的事情。 看到这个问题 。 一个解决方法是使用帮助程序类 template< typename T, typename U> struct FooDispatchHelper { static U dispatch( const Foo<T> * f ) { return f->template bar_internal<U>(); } }; template< typename T > struct FooDispa...

具有更多参数的模板(Template with more parameters)

只需专注于添加变量名称和支持任意数量的参数,您可以使用可变参数宏来获取名称和可变参数模板以获取所有参数。 它不会尝试集成写入序列,但这可以通过不直接使用operator<<()而非有条件地格式化值来轻松合并。 这是一个示例代码: #include <iostream> #include <sstream> #include <string> #include <tuple> #include <vector> template <int I, int N> struct debugger { ...

在模板函数参数之间强制实现所需的关系(Enforcing desired relationship between template function parameters)

如果你可以将枚举提升到编译时常数,那么它是可能的: template <MessageType E, class Data> void Send(Data const& ) { ... } 我们可以创建一个类模板,专门针对枚举所期望的每个枚举: template <MessageType E> struct expected_type; template <> struct expected_type<MessageType::TypeA> { using type = Foo; }; temp...

变量模板作为std :: function的参数(Variadic template as parameters to std::function)

当您像这样定义类模板时: template <typename T> struct A { A(T&& param) } 然后创建一个实例: A<int> myInstance(someIntVariable); 它不会编译 。 原因是, T的类型由您明确指定(作为int ,在A<int> ),并且您的类构造函数参数不再是T&& ,而是int&& ,因此它不再是通用引用(它同时接受左值和右值参考),但是常规右值参考。 接下来,如果您传递一些整数,则会出现类型不匹配错误,因为在期望rvalue...

指定一些模板参数(Specifying some template parameters)

std::cout << crossProduct(f, g) << std::endl; | | +------Creates a temporary object (rvalue) rvalues不能绑定到非const引用,因此请使用const引用 template <class T, int dim, bool isRowVec> std::ostream& operator<<(std::ostream& os...

模板模板参数和默认参数(Template template parameters and default arguments)

模板参数的参数将忽略默认参数。 在n3337中有这个例子,章节[temp.arg.template] ,第2段: template<class T> class A { /∗ ... ∗/ }; template<class T, class U = T> class B { /∗ ... ∗/ }; template <class ... Types> class C { /∗ ... ∗/ }; template<template<class> class P> class X { /∗ .....

基于运行时值调用不同的模板函数特化(Calling different template function specialisations based on a run-time value)

您对expected_type和Receive模板有正确的想法; 只剩下一步才能完成所有工作。 首先,我们需要给我们一些枚举MessageType的方法: enum class MessageType { _FIRST = 0, TypeA = _FIRST, TypeB, TypeM = 100, _LAST }; 然后我们可以在编译时枚举MessageType并生成调度函数(使用SFINAE跳过expected_types未定义的值): // this...

功能模板采用模板非类型模板参数(Function template taking a template non-type template parameter)

我认为这种符号是不可能的。 提议P0127R1使这种表示法成为可能。 模板将声明如下: template <auto P> void pmf_tparam(); // ... pmf_tparam<&S::member>(); pmf_tparam<&f>(); 为非类型参数添加auto的提议被投票到Oulu的C ++工作文件中,结果被投票成为在Oulu中也导致C ++ 17的CD 。 如果没有非类型参数的auto类型,则需要提供指针的类型: template <typename T, T P>...

函数与模板中的参数。(Function with arguments in a template. Django)

看起来好像是在混淆客户端代码(JavaScript)和服务器端(Django)。 要获取相关的用户ID,您可以在表单中添加一个额外的隐藏字段: {% for user in following % } <form method="post" action="{% url views.remove_relationship %}"> {% csrf_token %} <input type="hidden" name="user_id" value="{{ user.id }}"> <input ...

使用模板参数作为函数参数(Using template parameters as function parameters)

如果您的参数在编译时已知,您甚至不需要sfinae进行检查 - 一个简单的assert将提供更好的诊断。 但是,如果参数未知(更可能是场景),则不能使其成为模板参数,并且必须使用异常或返回错误代码来指示提供的参数不在支持的域中。 If your argument is known at compile time, you do not even need sfinae for your check - a simple assert will provide much better diagnos...

相关文章

更多

Solr4.7.2启动时的Index locked for write for core问题分析

Solr在启动时,通过多线程的方式加载core,在加载完每个core的配置文件后,实例化了一个Solr ...

《虚拟机系统与进程的通用平台》(Virtual Machines: Versatile Platforms for Systems and Processes)扫描版[PDF]

中文名: 虚拟机系统与进程的通用平台 原名: Virtual Machines: Versati ...

HDFS patch前后Ganglia看到running processes变化的分析

Ganglia running processes是怎么算出来的?ganglia是通过 cat /pr ...

在java中如何获取文件夹占用空间的大小?(注意这里不是问文件夹大小)

一般情况下,文件夹大小和文件夹占用空间是不一样的,如附件所示。 在java中,如果要求文件夹大小,可 ...

Solr4:Tomcat7与Solr之多核配置(Multiple Cores)

1. 背景 多核,官方说法,让你只用一个Solr实例,实现多配置多索引的功能,为不同的应用保留不同的 ...

response.getWriter.write()方法和response.getWriter.println()方法区别

response.getWriter.write(map)方法 response.getWriter ...

删除暴风文件夹内的stormliv.exe

要删除暴风文件夹内的stormliv.exe 需要先在任务管理器中关闭stormliv.exe 要彻底 ...

solr 做索引时报 Lock obtain timed out: SingleInstanceLock: write.lock

有个频繁做索引的应用,它同时也对外提供搜索服务。大部分是 solr 1.3 的默认配置。solr 做索 ...

Hadoop Core 学习笔记(一) SequenceFile文件写入和读取Writable数据

刚接触Hadoop时,对SequenceFile和Writable还产生了一点联想,以为是什么神奇的东 ...

Hadoop Core 学习笔记(二) lzo文件的写入和读取

压缩是绕不开的话题,因为当今很多程序的压力还是在IO.特别是Hadoop这种分布式存储和运算框架,单台 ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本