Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'greenlet' in functional components in Python. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
spawn(cmd, search_path=0)
# -- find greenlet but skip the one in "."
if not build:
oldpath = sys.path[:]
sys.path.remove(here)
import greenlet
if not build:
sys.path[:] = oldpath
sys.stdout.write("python %s (%s bit) using greenlet %s from %s\n" %
(sys.version.split()[0], bits(), greenlet.__version__, greenlet.__file__))
sys.stdout.flush()
# -- run tests
from tests import test_collector
result = unittest.TextTestRunner(verbosity=verbosity).run(test_collector(build_base))
if result.failures or result.errors:
sys.exit(1)
spawn(cmd, search_path=0)
# -- find greenlet but skip the one in "."
if not build:
oldpath = sys.path[:]
sys.path.remove(here)
import greenlet
if not build:
sys.path[:] = oldpath
sys.stdout.write("python %s (%s bit) using greenlet %s from %s\n" %
(sys.version.split()[0], bits(), greenlet.__version__, greenlet.__file__))
sys.stdout.flush()
# -- run tests
from tests import test_collector
result = unittest.TextTestRunner(verbosity=verbosity).run(test_collector(build_base))
if result.failures or result.errors:
sys.exit(1)
def test_switch_to_another_thread(self):
data = {}
error = None
created_event = threading.Event()
done_event = threading.Event()
def foo():
data['g'] = greenlet(lambda: None)
created_event.set()
done_event.wait()
thread = threading.Thread(target=foo)
thread.start()
created_event.wait()
try:
data['g'].switch()
except greenlet.error:
error = sys.exc_info()[1]
self.assertTrue(error != None, "greenlet.error was not raised!")
done_event.set()
thread.join()
except IndexError:
return "caught"
else:
return "normal exit"
def f2():
main.switch("from f2")
g1 = greenlet(f1)
g2 = greenlet(f2, parent=g1)
self.assertRaises(IndexError, g2.throw, IndexError)
self.assertTrue(g2.dead)
self.assertTrue(g1.dead)
g1 = greenlet(f1)
g2 = greenlet(f2, parent=g1)
res = g1.switch()
self.assertEqual(res, "f1 ready to catch")
res = g2.throw(IndexError)
self.assertEqual(res, "caught")
self.assertTrue(g2.dead)
self.assertTrue(g1.dead)
g1 = greenlet(f1)
g2 = greenlet(f2, parent=g1)
res = g1.switch()
self.assertEqual(res, "f1 ready to catch")
res = g2.switch()
self.assertEqual(res, "from f2")
res = g2.throw(IndexError)
self.assertEqual(res, "caught")
self.assertTrue(g2.dead)
gr_main.switch()
b()
spin(1)
def b():
spin(5)
gr_main.switch()
c()
def c():
spin(7)
GreenletProfiler.set_clock_type('cpu')
GreenletProfiler.start(builtins=True)
gr_main = greenlet.getcurrent()
g = greenlet.greenlet(a)
g.switch()
spin(2)
g.switch()
spin(3)
g.switch()
self.assertFalse(g, 'greenlet not complete')
GreenletProfiler.stop()
ystats = GreenletProfiler.get_func_stats()
# Check the stats for spin().
spin_stat = find_func(ystats, 'spin')
self.assertEqual(5, spin_stat.ncall)
self.assertAlmostEqual(18 * self.spin_cost, spin_stat.ttot,
places=2, msg="spin()'s total time is wrong")
def f():
greenlet.getcurrent().test = 42
def deldict(g):
def fmain(seen):
try:
greenlet.getcurrent().parent.switch()
except:
seen.append(sys.exc_info()[0])
raise
raise SomeError
def switchapply():
apply(greenlet.getcurrent().parent.switch, args, kwargs)
g = greenlet(switchapply)
greenlet.getcurrent() # update ts_current
lock2.release() # release gc
t = threading.Thread(target=another_thread)
t.start()
class gc_callback(object):
def __del__(self):
lock1.release()
lock2.acquire()
recycled[0] = True
class garbage(object):
def __init__(self):
self.cycle = self
self.callback = gc_callback()
l = []
x = range(N*2)
current = greenlet.getcurrent()
g = garbage()
for i in x:
g = None # lose reference to garbage
if recycled[0]:
# gc callback called prematurely
t.join()
return False
last = greenlet()
if recycled[0]:
break # yes! gc called in green_new
l.append(last) # increase allocation counter
else:
# gc callback not called when expected
gc.collect()
if recycled[0]:
t.join()
def worker():
# wait for the value
value = greenlet.getcurrent().parent.switch()
# delete all references to ourself
del worker[0]
initiator.parent = greenlet.getcurrent().parent
# switch to main with the value, but because
# ts_current is the last reference to us we
# return immediately
try:
greenlet.getcurrent().parent.switch(value)
finally:
seen.append(greenlet.getcurrent())
def initiator():