mirror of
https://opendev.org/x/pyghmi
synced 2025-01-15 20:27:45 +00:00
Fix mass thread initialization of sessions
There was a problem where the io thread could exist, but not yet be ready. Fix this by adding an iothreadready bool and a list of events to fire when iothread is ready. Change-Id: I4eb13e2210fa07bddbe717f56b12c736c99938dc
This commit is contained in:
parent
26f4ad8ec7
commit
9711d1edcf
@ -43,6 +43,8 @@ iothread = None # the thread in which all IO will be performed
|
||||
# This thread will tuck away the threading situation such that
|
||||
# calling code doesn't have to do any gymnastics to cope with
|
||||
# the nature of things.
|
||||
iothreadready = False # whether io thread is yet ready to work
|
||||
iothreadwaiters = [] # threads waiting for iothreadready
|
||||
ioqueue = collections.deque([])
|
||||
selectbreak = None
|
||||
selectdeadline = 0
|
||||
@ -50,7 +52,8 @@ running = True
|
||||
iosockets = [] # set of iosockets that will be shared amongst Session objects
|
||||
|
||||
|
||||
def _ioworker(initialized):
|
||||
def _ioworker():
|
||||
global iothreadready
|
||||
global selectbreak
|
||||
global selectdeadline
|
||||
selectbreak = os.pipe()
|
||||
@ -58,8 +61,11 @@ def _ioworker(initialized):
|
||||
iosockets.append(selectbreak[0])
|
||||
iowaiters = []
|
||||
timeout = 300
|
||||
initialized.set()
|
||||
iothreadready = True
|
||||
while running:
|
||||
while iothreadwaiters:
|
||||
waiter = iothreadwaiters.pop()
|
||||
waiter.set()
|
||||
if timeout < 0:
|
||||
timeout = 0
|
||||
selectdeadline = _monotonic_time() + timeout
|
||||
@ -230,13 +236,19 @@ class Session(object):
|
||||
def _createsocket(cls):
|
||||
global iowork
|
||||
global iothread
|
||||
global iothreadready
|
||||
global iosockets
|
||||
if iothread is None:
|
||||
initevt = threading.Event()
|
||||
iothread = threading.Thread(target=_ioworker, args=(initevt,))
|
||||
iothreadwaiters.append(initevt)
|
||||
iothread = threading.Thread(target=_ioworker)
|
||||
iothread.daemon = True
|
||||
iothread.start()
|
||||
initevt.wait()
|
||||
elif not iothreadready:
|
||||
initevt = threading.Event()
|
||||
iothreadwaiters.append(initevt)
|
||||
initevt.wait()
|
||||
atexit.register(cls._cleanup)
|
||||
cls.socket = _io_apply(socket.socket,
|
||||
(socket.AF_INET6, socket.SOCK_DGRAM)) # INET6
|
||||
|
Loading…
x
Reference in New Issue
Block a user