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 thezmq
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 thelogging
module.
原文:https://stackoverflow.com/questions/18624516
最满意答案
您可以将伴随对象与自定义“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)[2023-05-19]
这应该工作: 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.getClass.get ... -
Scala:隐式转换案例类对的通用解决方案(Scala: Generic solution to implicitly convert pairs of case classes)[2024-02-04]
它能解决你的问题,还是我误解了什么? 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` instanc ... -
在Scala中,是否有简单的方法将案例类转换为元组?(In Scala, is there an easy way to convert a case class into a tuple?)[2023-05-29]
如何在协同对象中调用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("test", ... -
需要明确的是:如果您的数据比哈希代码有更多位,则哈希冲突是不可避免的 ,因为有可能的值比可能的哈希代码更多。 但是,scala case类哈希代码使用的是MurmurHash3哈希算法,它为非加密哈希函数提供了非常好的哈希值分布。 因此案例类的哈希冲突应该是相对罕见的。 您仍然需要在代码中正确处理它们! 另一句话:您的代码必须正确处理哈希代码冲突。 但它们很少具有良好的散列函数。 因此,如果您真的想要彻底,那么您应该进行测试,其中您有意拥有多个具有相同哈希码的案例类。 Just to be clear: h ...
-
Scala和案例类(Scala and case classes)[2022-06-25]
如果你使用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.zipWithIndex) ... -
如果你不需要所有元素的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)) ) prin ...
-
你的问题很模糊,因为有很多方法可以做到这一点。 这是一个选择。 随着案例类的定义...... 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 { d ...
-
您可以查看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 follow ...
-
scala在case类之间转换的最佳方法是什么?(What would be the best way in scala to convert between case classes?)[2023-06-12]
您可以将伴随对象与自定义“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 ... -
使用Scala applicative将List [Object]转换为case类(Using Scala applicative to convert List[Object] to case class)[2023-05-31]
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 case Li ...