ianmcook commented on code in PR #37:
URL: https://github.com/apache/arrow-experiments/pull/37#discussion_r1763523109


##########
http/get_simple/python/server/fastapi_uvicorn/server.py:
##########
@@ -0,0 +1,86 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import pyarrow as pa
+from random import randbytes
+import io
+from fastapi import FastAPI
+from fastapi.responses import StreamingResponse
+
+schema = pa.schema([
+    ("a", pa.int64()),
+    ("b", pa.int64()),
+    ("c", pa.int64()),
+    ("d", pa.int64())
+])
+
+
+def GetPutData():
+    total_records = 100000000
+    length = 4096
+    ncolumns = 4
+
+    arrays = []
+
+    for x in range(0, ncolumns):
+        buffer = pa.py_buffer(randbytes(length * 8))
+        arrays.append(pa.Int64Array.from_buffers(
+            pa.int64(), length, [None, buffer], null_count=0))
+
+    batch = pa.record_batch(arrays, schema)
+    batches = []
+
+    records = 0
+    while records < total_records:
+        if records + length > total_records:
+            last_length = total_records - records
+            batches.append(batch.slice(0, last_length))
+            records += last_length
+        else:
+            batches.append(batch)
+            records += length
+
+    return batches
+
+
+def generate_bytes(schema, batches):
+    with pa.RecordBatchReader.from_batches(schema, batches) as source, \
+            io.BytesIO() as sink, \
+            pa.ipc.new_stream(sink, schema) as writer:
+        for batch in source:
+            sink.seek(0)
+            writer.write_batch(batch)
+            sink.truncate()
+            yield sink.getvalue()
+
+        sink.seek(0)
+        writer.close()
+        sink.truncate()
+        yield sink.getvalue()

Review Comment:
   <details>
     <summary>full traceback</summary>
     
     ```
     ERROR:    Exception in ASGI application
   Traceback (most recent call last):
     File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/responses.py", 
line 265, in __call__
       await wrap(partial(self.listen_for_disconnect, receive))
     File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/responses.py", 
line 261, in wrap
       await func()
     File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/responses.py", 
line 238, in listen_for_disconnect
       message = await receive()
                 ^^^^^^^^^^^^^^^
     File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py",
 line 553, in receive
       await self.message_event.wait()
     File "/opt/homebrew/anaconda3/lib/python3.11/asyncio/locks.py", line 213, 
in wait
       await fut
   asyncio.exceptions.CancelledError: Cancelled by cancel scope 10662f4d0
   
   During handling of the above exception, another exception occurred:
   
     + Exception Group Traceback (most recent call last):
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py",
 line 399, in run_asgi
     |     result = await app(  # type: ignore[func-returns-value]
     |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py",
 line 70, in __call__
     |     return await self.app(scope, receive, send)
     |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/fastapi/applications.py", 
line 1054, in __call__
     |     await super().__call__(scope, receive, send)
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/applications.py",
 line 123, in __call__
     |     await self.middleware_stack(scope, receive, send)
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/middleware/errors.py",
 line 186, in __call__
     |     raise exc
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/middleware/errors.py",
 line 164, in __call__
     |     await self.app(scope, receive, _send)
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/middleware/exceptions.py",
 line 65, in __call__
     |     await wrap_app_handling_exceptions(self.app, conn)(scope, receive, 
send)
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/_exception_handler.py",
 line 64, in wrapped_app
     |     raise exc
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/_exception_handler.py",
 line 53, in wrapped_app
     |     await app(scope, receive, sender)
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/routing.py", 
line 756, in __call__
     |     await self.middleware_stack(scope, receive, send)
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/routing.py", 
line 776, in app
     |     await route.handle(scope, receive, send)
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/routing.py", 
line 297, in handle
     |     await self.app(scope, receive, send)
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/routing.py", 
line 77, in app
     |     await wrap_app_handling_exceptions(app, request)(scope, receive, 
send)
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/_exception_handler.py",
 line 64, in wrapped_app
     |     raise exc
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/_exception_handler.py",
 line 53, in wrapped_app
     |     await app(scope, receive, sender)
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/routing.py", 
line 75, in app
     |     await response(scope, receive, send)
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/responses.py", 
line 258, in __call__
     |     async with anyio.create_task_group() as task_group:
     |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/anyio/_backends/_asyncio.py",
 line 678, in __aexit__
     |     raise BaseExceptionGroup(
     | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
     +-+---------------- 1 ----------------
       | Traceback (most recent call last):
       |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/responses.py", 
line 261, in wrap
       |     await func()
       |   File 
"/opt/homebrew/anaconda3/lib/python3.11/site-packages/starlette/responses.py", 
line 252, in stream_response
       |     chunk = chunk.encode(self.charset)
       |             ^^^^^^^^^^^^
       | AttributeError: 'memoryview' object has no attribute 'encode'
       +------------------------------------
   ```
   </details>



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to