Revision dcd511c8
b/lib/http/__init__.py | ||
---|---|---|
311 | 311 |
return serializer.LoadJson(data) |
312 | 312 |
|
313 | 313 |
|
314 |
def WaitForSocketCondition(sock, event, timeout): |
|
315 |
"""Waits for a condition to occur on the socket. |
|
316 |
|
|
317 |
@type sock: socket |
|
318 |
@param sock: Wait for events on this socket |
|
319 |
@type event: int |
|
320 |
@param event: ORed condition (see select module) |
|
321 |
@type timeout: float or None |
|
322 |
@param timeout: Timeout in seconds |
|
323 |
@rtype: int or None |
|
324 |
@return: None for timeout, otherwise occured conditions |
|
325 |
|
|
326 |
""" |
|
327 |
check = (event | select.POLLPRI | |
|
328 |
select.POLLNVAL | select.POLLHUP | select.POLLERR) |
|
329 |
|
|
330 |
if timeout is not None: |
|
331 |
# Poller object expects milliseconds |
|
332 |
timeout *= 1000 |
|
333 |
|
|
334 |
poller = select.poll() |
|
335 |
poller.register(sock, event) |
|
336 |
try: |
|
337 |
while True: |
|
338 |
# TODO: If the main thread receives a signal and we have no timeout, we |
|
339 |
# could wait forever. This should check a global "quit" flag or |
|
340 |
# something every so often. |
|
341 |
io_events = poller.poll(timeout) |
|
342 |
if not io_events: |
|
343 |
# Timeout |
|
344 |
return None |
|
345 |
for (_, evcond) in io_events: |
|
346 |
if evcond & check: |
|
347 |
return evcond |
|
348 |
finally: |
|
349 |
poller.unregister(sock) |
|
350 |
|
|
351 |
|
|
352 | 314 |
def SocketOperation(sock, op, arg1, timeout): |
353 | 315 |
"""Wrapper around socket functions. |
354 | 316 |
|
... | ... | |
399 | 361 |
else: |
400 | 362 |
wait_for_event = event_poll |
401 | 363 |
|
402 |
event = WaitForSocketCondition(sock, wait_for_event, timeout) |
|
364 |
event = utils.WaitForSocketCondition(sock, wait_for_event, timeout)
|
|
403 | 365 |
if event is None: |
404 | 366 |
raise HttpSocketTimeout() |
405 | 367 |
|
b/lib/http/client.py | ||
---|---|---|
37 | 37 |
|
38 | 38 |
from ganeti import workerpool |
39 | 39 |
from ganeti import http |
40 |
from ganeti import utils |
|
40 | 41 |
|
41 | 42 |
|
42 | 43 |
HTTP_CLIENT_THREADS = 10 |
... | ... | |
249 | 250 |
|
250 | 251 |
if not connected: |
251 | 252 |
# Wait for connection |
252 |
event = http.WaitForSocketCondition(self.sock, select.POLLOUT,
|
|
253 |
self.CONNECT_TIMEOUT) |
|
253 |
event = utils.WaitForSocketCondition(self.sock, select.POLLOUT,
|
|
254 |
self.CONNECT_TIMEOUT)
|
|
254 | 255 |
if event is None: |
255 | 256 |
raise http.HttpError("Timeout while connecting to server") |
256 | 257 |
|
b/lib/utils.py | ||
---|---|---|
1497 | 1497 |
return False |
1498 | 1498 |
|
1499 | 1499 |
|
1500 |
def WaitForSocketCondition(sock, event, timeout): |
|
1501 |
"""Waits for a condition to occur on the socket. |
|
1502 |
|
|
1503 |
@type sock: socket |
|
1504 |
@param sock: Wait for events on this socket |
|
1505 |
@type event: int |
|
1506 |
@param event: ORed condition (see select module) |
|
1507 |
@type timeout: float or None |
|
1508 |
@param timeout: Timeout in seconds |
|
1509 |
@rtype: int or None |
|
1510 |
@return: None for timeout, otherwise occured conditions |
|
1511 |
|
|
1512 |
""" |
|
1513 |
check = (event | select.POLLPRI | |
|
1514 |
select.POLLNVAL | select.POLLHUP | select.POLLERR) |
|
1515 |
|
|
1516 |
if timeout is not None: |
|
1517 |
# Poller object expects milliseconds |
|
1518 |
timeout *= 1000 |
|
1519 |
|
|
1520 |
poller = select.poll() |
|
1521 |
poller.register(sock, event) |
|
1522 |
try: |
|
1523 |
while True: |
|
1524 |
# TODO: If the main thread receives a signal and we have no timeout, we |
|
1525 |
# could wait forever. This should check a global "quit" flag or |
|
1526 |
# something every so often. |
|
1527 |
io_events = poller.poll(timeout) |
|
1528 |
if not io_events: |
|
1529 |
# Timeout |
|
1530 |
return None |
|
1531 |
for (_, evcond) in io_events: |
|
1532 |
if evcond & check: |
|
1533 |
return evcond |
|
1534 |
finally: |
|
1535 |
poller.unregister(sock) |
|
1536 |
|
|
1537 |
|
|
1500 | 1538 |
def partition(seq, pred=bool): # # pylint: disable-msg=W0622 |
1501 | 1539 |
"Partition a list in two, based on the given predicate" |
1502 | 1540 |
return (list(itertools.ifilter(pred, seq)), |
Also available in: Unified diff