Python:从多个进程写入单个文件(ZMQ)(Python: write to single file from multiple processes (ZMQ))

我想从多个进程写入单个文件。 确切地说,我宁愿不使用多处理队列解决方案进行多处理,因为有几个子模块由其他开发人员编写。 但是,对此类子模块的每次写入都与写入zmq队列相关联。 有没有办法将zmq消息重定向到文件? 具体来说,我正在寻找http://www.huyng.com/posts/python-logging-from-multiple-processes/的内容,而不使用logging模块。


I want to write to a single file from multiple processes. To be precise, I would rather not use the Multiple processing Queue solution for multiprocessing as there are several submodules written by other developers. However, each write to the file for such submodules is associated with a write to a zmq queue. Is there a way I can redirect the zmq messages to a file? Specifically I am looking for something along the lines of http://www.huyng.com/posts/python-logging-from-multiple-processes/ without using the logging module.


原文:https://stackoverflow.com/questions/18624516
2023-07-06 17:07

满意答案

您可以将伴随对象与自定义“apply”方法一起使用:

case class MyReturnedModel(reason: String)

object MyReturnedModel {
  def apply(mod: ReturnedModel) = MyReturnedModel(mod.reason.toString)
}

val data: ReturnedModel = ... // Some instance of ReturnedModel
val mr = MyReturnModel(data)

请注意,case类及其伴随对象需要位于同一个文件中才能使用。


You can use a companion object with a custom "apply" method:

case class MyReturnedModel(reason: String)

object MyReturnedModel {
  def apply(mod: ReturnedModel) = MyReturnedModel(mod.reason.toString)
}

val data: ReturnedModel = ... // Some instance of ReturnedModel
val mr = MyReturnModel(data)

Just note that the case class and its companion object need to be in the same file for this to work.

相关问答

更多

案例类在Scala中映射(Case class to map in Scala)

这应该工作: def getCCParams(cc: AnyRef) = (Map[String, Any]() /: cc.getClass.getDeclaredFields) {(a, f) => f.setAccessible(true) a + (f.getName -> f.get(cc)) } This should work: def getCCParams(cc: AnyRef) = (Map[String, Any]() /: cc.getClas...

Scala:隐式转换案例类对的通用解决方案(Scala: Generic solution to implicitly convert pairs of case classes)

它能解决你的问题,还是我误解了什么? implicit final def aToOlder(self: A2): A1 = A1(a = self.a) implicit final def bToOlder(self: B2): B1 = B1(b = self.b) 用法: val old: A1 = A1("John") val oldFromNew: A1 = A2("Doe") // This will be implicitly converted from the `A2` in...

在Scala中,是否有简单的方法将案例类转换为元组?(In Scala, is there an easy way to convert a case class into a tuple?)

如何在协同对象中调用unapply().get() ? case class Foo(foo:String, bar:Int) val (str, in) = Foo.unapply(Foo("test", 123)).get() How about calling unapply().get() in the companion object? case class Foo(foo:String, bar:Int) val (str, in) = Foo.unapply(Foo("te...

Scala案例类和Hash Collision(Scala case classes and Hash Collision)

需要明确的是:如果您的数据比哈希代码有更多位,则哈希冲突是不可避免的 ,因为有可能的值比可能的哈希代码更多。 但是,scala case类哈希代码使用的是MurmurHash3哈希算法,它为非加密哈希函数提供了非常好的哈希值分布。 因此案例类的哈希冲突应该是相对罕见的。 您仍然需要在代码中正确处理它们! 另一句话:您的代码必须正确处理哈希代码冲突。 但它们很少具有良好的散列函数。 因此,如果您真的想要彻底,那么您应该进行测试,其中您有意拥有多个具有相同哈希码的案例类。 Just to be cle...

Scala和案例类(Scala and case classes)

如果你使用Iterators你可以避免多次传递(因为它们是懒惰的),例如 for ((line, lineNum) <- testList.iterator.zipWithIndex) yield File(line, lineNum) If you use Iterators you can avoid multiple passes (because they are lazy), e.g. for ((line, lineNum) <- testList.iterator.zipWithI...

具有行为的Scala案例类(Case classes in Scala with behavior)

如果你不需要所有元素的c,我会使用一个惰性值 case class CaseB(a:Int,b:String,c:Boolean) case class CaseA(a:Int,b:List[CaseB]){ lazy val c = b exists (_.c) } val ca = CaseA(42, List( CaseB(1,"hello",false), CaseB(2,",",false), CaseB(3,"world",true)) ) ...

是否可以在两个具有不同名称和scala中略有不同字段的案例类之间进行转换(Is it possible to convert between two case class with different name and slightly different field in scala)

你的问题很模糊,因为有很多方法可以做到这一点。 这是一个选择。 随着案例类的定义...... case class Foo(timestamp:String, epochSecond: Long) case class Bar(epochSecond: Long, timestamp:String) ...添加辅助工厂方法。 object Foo { def apply(b: Bar): Foo = this(b.timestamp, b.epochSecond) } object Bar ...

Scala案例类背后的内容是什么?(What's behind Scala case classes?)

您可以查看scala规范中的第5.3.2章“案例类”。 如果我正确总结,则会自动生成以下内容: 为类元素生成访问器 自动生成带apply / unapply的提取器对象 方法副本会自动添加 方法equals,hashCode,toString被覆盖 您可以比较scala源和生成的java字节码。 You can have a look at chapter 5.3.2 "Case Classes" in the scala spec. If I summarize correctly, the f...

scala在case类之间转换的最佳方法是什么?(What would be the best way in scala to convert between case classes?)

您可以将伴随对象与自定义“apply”方法一起使用: case class MyReturnedModel(reason: String) object MyReturnedModel { def apply(mod: ReturnedModel) = MyReturnedModel(mod.reason.toString) } val data: ReturnedModel = ... // Some instance of ReturnedModel val mr = MyReturn...

使用Scala applicative将List [Object]转换为case类(Using Scala applicative to convert List[Object] to case class)

result.collect {case name::date::Nil => Person(name.toString, LocalDate.parse(date.toString)} // or case List(name,date) => ... result.collect {case name::date::Nil => Person(name.toString, LocalDate.parse(date.toString)} // or ca...

相关文章

更多

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

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

storm0.9-single

参考 http://shiyanjun.cn/archives/241.html 用新版包自己做了一遍 ...

storm学习之Netty代替ZMQ

整理自 http://www.csdn.net/article/2014-08-04/2821018/ ...

Java 流(Stream)、文件(File)和IO

Java 流(Stream)、文件(File)和IO Java.io包几乎包含了所有操作输入、输 ...

【HDFS】HADOOP DISTRIBUTED FILE SYSTEM

【HDFS】Hadoop DISTRIBUTED FILE SYSTEM THE CAST CLIEN ...

用‘button’跟‘text’组合代替‘file’,选择文件后点‘submit’,‘file’的值被清空

各位大虾晚上好,我有个问题想请教你们,我想美化html的file外观,但貌似现在还不能用css直接设计 ...

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

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

PHP 中dirname(_file_)

PHP 中dirname(_file_) 2007-5-3 16:00|查看: 19256|评论: ...

xxx is not in the sudoers file解决方法

用sudo时提示&quot;xxx is not in the sudoers file. This ...

Hadoop 1.2.1 单节点安装(Single Node Setup)步骤

为了使您快速体验Hadoop,可以在一台机子上安装单节点(Node)的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}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本