Skip to content

Commit 3fefbb5

Browse files
committed
fix: refactor
1 parent 8527eb2 commit 3fefbb5

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

runpod/serverless/modules/rp_job.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,23 @@ async def run_job_generator(
184184
Run generator job used to stream output.
185185
Yields output partials from the generator.
186186
'''
187+
is_async_gen = inspect.isasyncgenfunction(handler)
188+
log.debug('Async Generator' if is_async_gen else 'Standard Generator', job["id"])
189+
187190
try:
188191
job_output = handler(job)
189-
if inspect.isasyncgenfunction(handler):
190-
log.debug('Async generator', job["id"])
192+
193+
if is_async_gen:
191194
async for output_partial in job_output:
192195
yield {"output": output_partial}
193196
else:
194-
log.debug('Generator', job["id"])
195197
for output_partial in job_output:
196198
yield {"output": output_partial}
199+
197200
except Exception as err: # pylint: disable=broad-except
198201
log.error(err, job["id"])
199-
yield {"error": f"handler: {str(err)} \ntraceback: {traceback.format_exc()}"}
202+
yield {
203+
"error": f"handler: {str(err)} \ntraceback: {traceback.format_exc()}"
204+
}
200205
finally:
201206
log.info('Finished running generator.', job["id"])

tests/test_serverless/test_worker.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,23 @@ def generator_handler_exception(job):
164164
raise Exception() # pylint: disable=broad-exception-raised
165165

166166

167+
def test_generator_handler_exception():
168+
""" Test generator_handler_exception """
169+
job = {"id": "test_job"}
170+
gen = generator_handler_exception(job)
171+
172+
# Process the first yielded value
173+
output = next(gen)
174+
assert output == "test1", "First output should be 'test1'"
175+
176+
# Attempt to get the next value, expecting an exception
177+
try:
178+
next(gen)
179+
assert False, "Expected an exception to be raised"
180+
except Exception: # pylint: disable=broad-except
181+
assert True, "Exception was caught as expected"
182+
183+
167184
class TestRunWorker(IsolatedAsyncioTestCase):
168185
""" Tests for runpod | serverless| worker """
169186

0 commit comments

Comments
 (0)