def f(): g1 = greenlet(fmain) g1.switch(seen) someref.append(g1) del g1 lock.release() lock2.acquire() greenlet() # trigger release lock.release() lock2.acquire()
def test_dealloc(self): seen = [] g1 = greenlet(fmain) g2 = greenlet(fmain) g1.switch(seen) g2.switch(seen) assert seen == [] del g1 assert seen == [greenlet.GreenletExit] del g2 assert seen == [greenlet.GreenletExit, greenlet.GreenletExit]
def test_exception(self): seen = [] g1 = greenlet(fmain) g2 = greenlet(fmain) g1.switch(seen) g2.switch(seen) g2.parent = g1 assert seen == [] self.failUnlessRaises(SomeError, g2.switch) assert seen == [SomeError] g2.switch() assert seen == [SomeError]
def test_simple(self): lst = [] def f(): lst.append(1) greenlet.getcurrent().parent.switch() lst.append(3) g = greenlet(f) lst.append(0) g.switch() lst.append(2) g.switch() lst.append(4) assert lst == range(5)
def test_frame(self): def f1(): f = sys._getframe(0) assert f.f_back is None greenlet.getcurrent().parent.switch(f) return "meaning of life" g = greenlet(f1) frame = g.switch() assert frame is g.gr_frame assert g next = g.switch() assert not g assert next == "meaning of life" assert g.gr_frame is None
def optimize(self): input_var_names = [var_obj.name for var_obj in self.input_vars] # FIXME: Do we want to create shadows with multiple names? self._working_context = self.context.create_shadow(input_var_names[0]) for var_name in input_var_names: self._working_context[var_name] = self.context[var_name][:] sub_block = self.block.restrict(inputs=input_var_names, outputs=[self.objective_var, self.constraint_var]) input_len = len(self._working_context[input_var_names[0]]) cobyla_instances = [COBYLAInstance(index=i, optimizer=self) for \ i in range(input_len)] greenlet_list = [greenlet(cobyla_instance.minimize_at_index) for \ cobyla_instance in cobyla_instances] self.cobyla_status_arr = zeros(input_len, dtype=bool) active_greenlets = input_len while active_greenlets > 0: active_greenlets = 0 for greenlet_inst in greenlet_list: if not greenlet_inst.dead: active_greenlets += 1 greenlet_inst.switch() sub_block.execute(self._working_context) self.context['plot_index'] = arange(input_len) input_plots = [PlotConfig(number=plot_num, x = 'plot_index', y=var_name, type='Line') for \ plot_num, var_name in enumerate(input_var_names)] plot_configs = input_plots + [ PlotConfig(number=len(input_var_names), x='plot_index', y=self.objective_var, type='Line'), PlotConfig(number=len(input_var_names) + 1, x='plot_index', y=self.constraint_var, type='Line'),] result_view = View(Item('context', editor=ContextPlotEditor(#view_shadows=True, plot_configs=plot_configs, ), show_label=False), width=500, height=500, resizable=True) self.edit_traits(view=result_view, kind='modal') return
def test_send_exception(self): seen = [] g1 = greenlet(fmain) g1.switch(seen) self.failUnlessRaises(KeyError, self.send_exception, g1, KeyError) assert seen == [KeyError]
def send_exception(self, g, exc): def crasher(exc): raise exc g1 = greenlet(crasher, parent=g) g1.switch(exc)