首页
\
问答
\
将参数从类传递到继承自threading.Thread的另一个类(Pass arguments from a class to another class that inherits from threading.Thread)
将参数从类传递到继承自threading.Thread的另一个类(Pass arguments from a class to another class that inherits from threading.Thread)
我的python代码中有两个类。
Filtro
类必须向Filtro
类发送两个参数,但是如果我通过costructor发送它们,我永远不会调用线程metodstart
costructor。 这是我的代码:import threading class Man_Thr(threading.Thread): def __init__(self,cmd,q): self.comando=cmd self.coda=q def run(self): try: proc=subprocess.Popen([self.comando], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) self.coda.put(proc) except: print "Il comando", self.comando, "non esiste. Riprovare." self.coda.put(-1) class Filtro(Man_Thr): def __init__(self,cmd): q=Queue.Queue() thr=Man_Thr(cmd,q) thr.start() self.result=q.get() filtro=Filtro(' '.join(sys.argv[1:len(sys.argv)-1]))
这是我的输出:
Traceback (most recent call last): File "filtro2.py", line 75, in <module> filtro=Filtro(' '.join(sys.argv[1:len(sys.argv)-1])) File "filtro2.py", line 45, in __init__ thr.start() File "/usr/lib64/python2.6/threading.py", line 465, in start raise RuntimeError("thread.__init__() not called") RuntimeError: thread.__init__() not called
我以这种方式运行脚本:
python filtro2.py ./a.out input.txt
将参数从派生类发送到基类的最佳方法是什么?
I have two class in my python code. The class
Filtro
must to send two parameters to the classMan_Thr
but if I send them by the costructor I never call the costructor for the thread metodstart
. This is my code:import threading class Man_Thr(threading.Thread): def __init__(self,cmd,q): self.comando=cmd self.coda=q def run(self): try: proc=subprocess.Popen([self.comando], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) self.coda.put(proc) except: print "Il comando", self.comando, "non esiste. Riprovare." self.coda.put(-1) class Filtro(Man_Thr): def __init__(self,cmd): q=Queue.Queue() thr=Man_Thr(cmd,q) thr.start() self.result=q.get() filtro=Filtro(' '.join(sys.argv[1:len(sys.argv)-1]))
and this is my output:
Traceback (most recent call last): File "filtro2.py", line 75, in <module> filtro=Filtro(' '.join(sys.argv[1:len(sys.argv)-1])) File "filtro2.py", line 45, in __init__ thr.start() File "/usr/lib64/python2.6/threading.py", line 465, in start raise RuntimeError("thread.__init__() not called") RuntimeError: thread.__init__() not called
I run the script in this way:
python filtro2.py ./a.out input.txt
What's the best method for send the parameters from a derivated class to a base class?
原文:https://stackoverflow.com/questions/30197023
更新时间:2023-11-04 20:11
最满意答案
相关问答
更多-
您可以使用反射来查找当前程序集中的所有控制器,然后找到不使用NonAction属性进行装饰的公共方法。 Assembly asm = Assembly.GetExecutingAssembly(); asm.GetTypes() .Where(type=> typeof(Controller).IsAssignableFrom(type)) //filter controllers .SelectMany(type => type.GetMethods()) .Where(meth ...
-
你可能想使用forward/4 : forward "/child", ChildWeb.Router You probably want to use forward/4: forward "/child", ChildWeb.Router
-
我为此使用RequireJS。 还有一个提供者,比如说lazyProvider: 在你懒惰的提供者...... this.resolve = function(controller){ return { myCtrl: ['$q',function ($q){ var defer = $q.defer(); require(['/app/controllers/'+controller,'myApp'],function(ctrl,myApp){ ...
-
允许路由器从数据库中查找控制器是一个好主意吗?(Is it a good idea to allow the router to look up controllers from a database?)[2022-03-08]
我看到的一些解决方案是明确指定与控制器/动作的路由“连接”。 例如,在NiceDog中 ,您可以指定路径 R('venues?/directions') ->controller('VenueController') ->action('directionsAction') ->on('GET'); 允许正则表达式匹配URL。 (上面的表达式将匹配venue/directions或venues/directions )我喜欢这种方法,因为它将控制器命名方案与URL命名方案分离。 另一种基于配置 ... -
不完全确定你在这里做错了什么。 也许还包括第二个网址的代码。 您可以使用Html.ActionLink方法的这个重载 : public static MvcHtmlString ActionLink( this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, Object routeValues, Object htmlAttributes ) ...
-
今天,您可以通过以下途径访问您的路线中的控制器(和路由操作): this.controllerFor('posts') 在您的控制器中,您声明对其他控制器的依赖关系: App.Post = Ember.ObjectController.extend({ needs: ['posts'] }); 然后,在你的控制器里面,你也可以使用this.controllerFor 。 仅供参考,我们计划使this.get('controllers.posts')在使用needs的控制器内部工作,这将使得在模板中直 ...
-
两个不同命名空间的控制器,每个控制器中的不同操作(Two Controllers in Different Namespaces, Different Actions In Each Controller)[2022-04-21]
不要认为这是可能的,因为它总是在它找到的第一个控制器/路径中寻找动作。 您需要添加额外的识别信息:“url:”controller1 / {controller} / {action} / {id}“”,或者可能创建另一个充当代理的控制器(扩展您想要优先选择的控制器并添加来自第二控制器的额外缺失方法): NewController:Site1.Controllers.Controller 或者您甚至可以使用反射并创建一个控制器,根据传递的参数为您解析逻辑。 Don't think that is possi ... -
$路由器: 为了在商店操作中访问$router ,你可以这样做: 将您的路由器声明移动到名为router.ts的单独文件 const router = new VueRouter({ routes, linkActiveClass: "active" }); export default router; 在actions.ts导入你的router ,并用它代替this.$router like this: import router from './router'; // then use th ...
-
我不完全确定,但我认为问题是你的初始化程序(假设你使用的是最新的Backbone.Marionette版本): PlugUI.addInitializer(function(options){ Status.router = new Status.Router({ controller: PlugUI.Status }); PlugUI.vent.trigger("routing:started"); }) 请尝试使用Status.addInitializer Stat ...
-
为了回答你的问题,我不会违反关注点。 路由器不应该担心控制器处理它的请求,或者说控制器是如何形成的。 它只需要知道在某些时候,即使在404的情况下,某些控制器也会处理它。 现在将一个控制器注入到路由中就可以了,然后你就可以将它原型化为一个类似的接口, public function dispatch(ControllerInterface $Controller){ ..... } 如果你需要第二个控制器怎么办? 例如,假设您需要管理员控制器和公共控制器以及成员控制器。 然后你建立3个路由器。 我个 ...