简体   繁体   English

Python:将特定对象的函数作为参数传递的危险是什么

[英]Python: What are the dangers of passing a specific object's function as an argument

In the main code, I have an instance of a class called "debugPrinterObject". 在主代码中,我有一个名为“ debugPrinterObject”的类的实例。 After instantiating it, I pass one of it's functions as an argument to another class called "bSoupProcessor" which processes text. 实例化它之后,我将其中一个函数作为参数传递给另一个称为“ bSoupProcessor”的类,该类处理文本。 Any logging information is saved to a text file using the function passed into the constructor of the bSoupProcessor class. 使用传递到bSoupProcessor类的构造函数中的函数,所有日志记录信息都保存到文本文件中。

This is done so that the file is held open by the debugPrinterObject, and editable through the function passed as an argument. 这样做是为了使文件由debugPrinterObject保持打开状态,并且可以通过作为参数传递的函数进行编辑。 the text file is only closed at the end of the program. 文本文件仅在程序结束时关闭。

It is working so far. 到目前为止,它正在工作。 I am going to implement multi threading, where there will be multiple "bSoupProcessor" classes, and they will all be using the same function of the "debugPrinterObject". 我将实现多线程,其中将有多个“ bSoupProcessor”类,并且它们都将使用“ debugPrinterObject”的相同功能。 Is this possible? 这可能吗? are there any problems/risks? 有任何问题/风险吗?

The only dangers are the typical concurrency issues you'd face in this situation. 唯一的危险是您在这种情况下会遇到的典型并发问题。 Be sure to either use Lock objects inside your logging method, or use them in bSoupProcessor before calling it. 确保在您的日志记录方法中使用Lock对象,或者在调用它之前在bSoupProcessor中使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM