I tried non-blocking I / O Eventlet behavior in Python

I tried Eventlet operation as non-blocking I / O programming in Python. However, it is difficult to write from scratch while understanding the behavior of Eventlet, so I referred to the implementation code on the OpenStack Nove_compute side. Live_migration on the Nove_compute side

(1) Eventlet sample code

First, a sample program that only operates the user-side threads (green threads) independently. .. ..

sample1.py


import eventlet
import time
eventlet.monkey_patch()

def _sample_processing():
    print("### Sample processing thread has started")
    for count in range(5):
        print("." *count)
        time.sleep(0.5)
    print("### Sample processing thread has finished")


def start_sample():
    opthread = eventlet.spawn(_sample_processing)
    opthread.wait()


if __name__ == '__main__':
    start_sample()

Try to move

$ python sample1.py 
### Sample processing thread has started

.
..
...
....
### Sample processing thread has finished

Next, assuming that you want to operate multiple green threads and link the operations, expand a little. .. ..

sample2.py


import eventlet
import time
eventlet.monkey_patch()

def _sample_processing():
    print("### Sample processing thread has started")
    for count in range(5):
        print("." *count)
        time.sleep(0.5)
    print("### Sample processing thread has finished")


def start_sample():
    opthread = eventlet.spawn(_sample_processing)
    finish_event = eventlet.event.Event()

    def thread_finished(thread, event):
        print("### Sample processing thread notification")
        event.send()
    opthread.link(thread_finished, finish_event)

    time.sleep(0)
    _sample_processing_monitor(finish_event)


def _sample_processing_monitor(finish_event):
    while True:
        if not finish_event.ready():
            print("+++ thread is still running!![{}]".format(finish_event._result))
        else:
            print("+++ thread is done!![{}]".format(finish_event._result))
            break
        time.sleep(1)


if __name__ == '__main__':
    start_sample()

I tried to move

$ python sample2.py 
### Sample processing thread has started

+++ thread is still running!![NOT_USED]
.
+++ thread is still running!![NOT_USED]
..
...
+++ thread is still running!![NOT_USED]
....
### Sample processing thread has finished
### Sample processing thread notification
+++ thread is done!![None]

With a nice feeling, I can now observe the completion of green thread operations on the _sample_processing side.

(2) DeepDive of Event class internal structure

If you check the internal mechanism of Event class of Eventlet,

class Event(object):
    ...

    _result = None
    _exc = None

    def __init__(self):
        self._waiters = set()
        self.reset()

    def __str__(self):
        params = (self.__class__.__name__, hex(id(self)),
                  self._result, self._exc, len(self._waiters))
        return '<%s at %s result=%r _exc=%r _waiters[%d]>' % params

    def reset(self):
        assert self._result is not NOT_USED, 'Trying to re-reset() a fresh event.'
        self._result = NOT_USED
        self._exc = None

    def ready(self):
        return self._result is not NOT_USED

    ...

    def send(self, result=None, exc=None):
        assert self._result is NOT_USED, 'Trying to re-send() an already-triggered event.'
        self._result = result
        if exc is not None and not isinstance(exc, tuple):
            exc = (exc, )
        self._exc = exc
        hub = hubs.get_hub()
        for waiter in self._waiters:
            hub.schedule_call_global(
                0, self._do_send, self._result, self._exc, waiter)

It has become.

At first, in sample2.py, if you just instantiate the Event class, self._result = NOT_USED The instance variable is retained as, but then when event.send () is invoked, self._result = None Changes to.

Finally, by looking up the contents of self._result on the monitoring side (_sample_processing_monitor), it seems that it will be possible to determine the completion of the green thread operations on the _sample_processing side.

Recommended Posts

I tried non-blocking I / O Eventlet behavior in Python
I tried Line notification in Python
I tried to implement PLSA in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
I tried using Bayesian Optimization in Python
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried playing a typing game in Python
I tried Python> autopep8
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
[Memo] I tried a pivot table in Python
I tried to implement TOPIC MODEL in Python
I tried adding a Python3 module in C
I tried to implement selection sort in python
I tried Python> decorator
I tried to graph the packages installed in Python
Basics of I / O screen using tkinter in python3
I tried using TradeWave (BitCoin system trading in Python)
I tried to implement a pseudo pachislot in Python
I tried to implement Dragon Quest poker in Python
I tried to implement GA (genetic algorithm) in Python
I tried to summarize how to use pandas in python
Storage I / O notes in Python with Azure Functions
I tried fp-growth with python
I wrote python in Japanese
Asynchronous I / O and non-blocking I / O
I tried Python C extension
[Python] I tried using OpenPose
I tried gRPC with Python
I tried scraping with python
I understand Python in Japanese!
What I learned in Python
I tried [scraping] fashion images and text sentences in Python.
I tried the accuracy of three Stirling's approximations in python
I tried to create API list.csv in Python from swagger.yaml
I tried to implement a one-dimensional cellular automaton in Python
I tried "a program that removes duplicate statements in Python"
I tried "How to get a method decorated in Python"
I tried programming the chi-square test in Python and Java.
I created a class in Python and tried duck typing
I tried to implement the mail sending function in Python
I tried to make a stopwatch using tkinter in python
I tried to implement blackjack of card game in Python
Behavior when listing in Python heapq
I tried web scraping with python.
Make standard output non-blocking in Python
I wrote Fizz Buzz in Python
I tried running GAN in Colaboratory
I tried using Thonny (Python / IDE)
I tried Grumpy (Go running Python).
I learned about processes in Python
I can't install scikit-learn in Python
I wrote the queue in Python
web coder tried excel in Python
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
[Python] I tried using YOLO v3
I wrote the stack in Python
Movement that changes direction in the coordinate system I tried Python 3