from gevent import Greenlet def my_function(x, y): print(x + y) return gr1 = Greenlet.spawn(my_function, 3, 4) gr2 = Greenlet.spawn(my_function, 5, 6) gr1.join() gr2.join()
from gevent import Greenlet import time def my_function(): for i in range(5): print("Hello") time.sleep(0.5) return gr1 = Greenlet.spawn(my_function) gr2 = Greenlet.spawn(my_function) gr1.join() gr2.join()In this example, we define a function called "my_function" that simply prints out "Hello" five times with a half-second delay in between each print. We then spawn two greenlets, "gr1" and "gr2," that both call "my_function." The program waits for both greenlets to finish executing before ending. Overall, these examples demonstrate how gevent.Greenlet.spawn can be used to execute multiple functions in parallel, thereby improving performance and efficiency.