Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(602)

Side by Side Diff: mojo/python/mojo_utils/data_pipe_utils.py

Issue 952223002: Add Python util to read from a data pipe into a bytearray (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Add comments Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/python/mojo_utils/__init__.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import mojo_system
6
7 def BlockingCopyFromDataPipeIntoString(data_pipe, deadline):
8 """
9 Reads from |data_pipe| until either (1) there is no more data to read or
10 (2) an error is encountered.
11 Returns a tuple of the result status and output string. If the status is not
12 mojo_system.RESULT_OK, the returned string will be None.
13 """
14 BUFFER_SIZE = 1000
qsr 2015/02/25 15:22:35 Use a power of 2, usual values are 1 to 4k
blundell 2015/02/26 16:33:53 Done.
15 input_bytes = bytearray(BUFFER_SIZE)
16 output_bytes = bytearray()
qsr 2015/02/25 15:22:35 Instead of transferring in BUFFER_SIZE chunk, beca
blundell 2015/02/26 16:33:53 Done.
17 while True:
18 status, read_bytes = data_pipe.ReadData(input_bytes)
19 if status == mojo_system.RESULT_OK:
20 output_bytes = output_bytes + read_bytes
21 continue
22
23 if status == mojo_system.RESULT_SHOULD_WAIT:
24 status, _ = data_pipe.Wait(mojo_system.HANDLE_SIGNAL_READABLE, deadline)
25
26 # If the wait results in RESULT_OK, try to read more bytes.
27 if status == mojo_system.RESULT_OK:
28 continue
qsr 2015/02/25 15:22:35 What happen is you get another result here? In par
blundell 2015/02/26 16:33:53 Any result other than OK or FAILED_PRECONDITION is
qsr 2015/02/26 16:45:19 Hum, misread the alignment. Ok.
29
30 # Treat a failed precondition as EOF.
31 if status == mojo_system.RESULT_FAILED_PRECONDITION:
32 return (mojo_system.RESULT_OK, output_bytes.decode("utf-8"))
qsr 2015/02/25 15:22:35 This method being a generic utility method, it sho
blundell 2015/02/26 16:33:52 Done.
33
34 return (status, None)
OLDNEW
« no previous file with comments | « mojo/python/mojo_utils/__init__.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698