Last active
June 9, 2020 00:27
-
-
Save abadger/bfd55741c281ccb534f7bbc8fe9b6202 to your computer and use it in GitHub Desktop.
Attempt to split up pydantic calls amongst multiple processes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import asyncio | |
import pickle | |
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor | |
import pydantic as p | |
class Model(p.BaseModel): | |
class Config: | |
extra = p.Extra.forbid | |
digit: p.constr(regex='^[0-9]*$') | |
def normalize(data): | |
out = Model(digit=data) | |
print(data) | |
return out | |
def workaround(data): | |
try: | |
out = Model(digit=data) | |
except Exception as e: | |
raise ValueError('pydantic Validation error: %s' % str(e)) | |
print(data) | |
return out | |
async def main(): | |
normalizers = [] | |
loop = asyncio.get_running_loop() | |
executor = ProcessPoolExecutor(max_workers=1) | |
for i in ('a', '1', '2'): | |
normalizers.append(loop.run_in_executor(executor, normalize, i)) | |
return await asyncio.gather(*normalizers, return_exceptions=True) | |
models = asyncio.run(main()) | |
print(models) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Output when using normalize() | |
1 | |
2 | |
[BrokenProcessPool('A process in the process pool was terminated abruptly while the future was running or pending.'), | |
BrokenProcessPool('A process in the process pool was terminated abruptly while the future was running or pending.'), | |
BrokenProcessPool('A process in the process pool was terminated abruptly while the future was running or pending.')] | |
# Output from workaround() | |
1 | |
2 | |
[ValueError('pydantic Validation error: 1 validation error for Model\ndigit\n string does not match regex "^[0-9]*$" (type=value_error.str.regex; pattern=^[0-9]*$)'), | |
Model(digit='1'), Model(digit='2')] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment