On 5 October 2010 15:48, Jim Easterbrook <jim.easterbr...@bbc.co.uk> wrote: > I've been tearing my hair out this afternoon
:-( > trying to get > Kamaelia.Internet.Selector.Selector to work. Finally I twigged that the > newly created Selector component was never being activated. I didn't > immediately spot that the TCPClient example code uses 'yield > newComponent()' so its parent component will activate the Selector. > > The component I'm altering to use Selector was a threaded component, so > it didn't have any yield statements. Putting in explicit activation of > the newly created Selector made everything magically spring to life. Now > I've got Selector working I've converted it to a non-threaded component. > > Which brings me on to my question. Is there any reason to prefer > yielding a newComponent message over explicit activation when creating > new child components? .activate() is preferred. yielding a newComponent was viewed as an easy/sensible way of passing back a message to the scheduler in Kamaelia's early days, long before the introduction of threaded components, whilst it still works, it's not the preferred approach. Care is taken to make sure that .activate() is threadsafe, so it can be happily and safely called. Creation doesn't cause activation because it often causes more problems than it's worth. (eg components being created inside a pipeline, starting executing before the linkages have been created) However, components rarely standalone, so .activate() returns the object. This means you can type this: foo = MyComponent().activate() And that has the same effect as this: foo = MyComponent() foo.activate() In threaded components, .activate() starts the main thread running *and* the generator to pump the data queues. For generator based components .activate() adds the component to a Queue.Queue which the scheduler looks inside and adds to an internal runqueue. (Hence why .activate() is threadsafe) The reason that code is still in TCPClient is based on the "if it isn't broke, don't fix it principle". (aka the "life's too short" principle :-) That said, I do periodically go through rip out bad code, and replace with new better idioms. TCPClient predates kamaelia having threaded components. (Indeed the first threaded component was a threaded version of TCPClient that was written for python on Series 60 mobile phones before "select" became available on Series 60 phones) Hope that helps, Michael. -- You received this message because you are subscribed to the Google Groups "kamaelia" group. To post to this group, send email to kamae...@googlegroups.com. To unsubscribe from this group, send email to kamaelia+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/kamaelia?hl=en.