The hub forms the Eventlet event loop, which distributes I/O events, dispatch greenthread. The presence of the hub allows the association to be promoted to greenthreads.
Eventlet has multiple hub implementations, so you should choose the one that best suits your system before you use it:
-
Epolls
-
Python 2.6 or Python-epoll package is required and only Linux is supported. This is the fastest pure Python hub.
-
Poll
-
All platforms that support it are available.
-
Selects
-
It can be used anywhere.
-
Pyevent
-
Based on the Libevent backend, the fastest! The default is not supported because it does not support native threads, but you can turn on pyevent.
-
If the hub you chose is not the ideal one, you can switch to another hub. It can be implemented either through the environment variable Eventlet_hub or by calling Use_hub ().
- Eventlet.hubs.use_hub (Hub=none)
-
Decide which hub to use, call the function using the name of the target hub module. This function should be called before any I/O is started by the app. Calling this function will erase any old hub, and any file descriptors and timers it manages, usually put this function on the first line of the main module called:
"""" "fromimport hubshubs.use_hub ("pyevent ")
The hubs implementation is within the thread, andEventlet.hubs.use_hub () only works within the current thread. When multithreading is used and these threads have their own hub, the function needs to be called before each thread function that requires a particular hub. You do not actually need to specify a hub for each thread, you can specify a specific hub for the master thread, and other threads to use the default hub. This hybrid configuration works as well.
You can also use a third-party hub module to replace the built-in, as long as the block itself is passed to Eventlet.hubs.use_hub (). For example:
from Import hubs from Import Myhubhubs.use_hub (Myhub)
How does the hub work?
The hub has a master greenlet,mainloop. When a running process requires an I/O operation, it registers a listener in the hub (so that the hub knows when to wake it), and then switches to Mainloop (via get_hub (). Switch()). If there are other processes ready to run, mainloop switches to them and switches control to mainloop when they complete execution or require I/O. In this way, Mainloop ensures that every process can be dispatched when it has to be completed.
The Mainloop is only executed when the first I/O occurs, and it is not the greenlet that the __main__ is running on. This lazy way of running explains why you do not need to explicitly call the dispatch () method, which means that the code does not need to be refactored to start using Eventlet.
More hub-related functions
- Eventlet.hubs.get_hub ()
-
-
Gets the current event hub object.
-
Note:
-
This is considered an internal API and can therefore be accidentally changed without being deprecated.
-
- Eventlet.hubs.get_default_hub ()
-
-
Select The default hub implementation based on what multiplexing libraries is installed. The order, the hubs is tried is:
-
The default hub implementation is selected automatically, and the order of the attempts is: Epoll, Kqueue, poll, select, where Pyevent hub is not automatically selected because it is not a Python thread-safe.
-
Note:
-
This is considered an internal API and can therefore be accidentally changed without being deprecated.
Eventlet.hubs.trampoline (FD, Read=none, Write=none, Timeout=none, timeout_exc=< class eventlet.timeout.timeout >,mark_as_closed=none)
-
Pauses the current coprocessor until the given socket or file description Word is ready
read or
write or the specified
timeout has passed. To wait for the
FD to be ready to read, pass
in the read =true, and wait for the
FD to be ready to write to the
write =true; To specify a time-out limit, pass in the parameter
timeout(seconds)
.
- If the timeout limit is reached before the socket is ready to read or write, it throws a
timeout_exc exception instead of returning normally.
- Note:
- This is considered an internal API and can therefore be accidentally changed without being deprecated.
Python--eventlet.hubs