Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement serialization protocol in SwaggerClient class? #471

Open
dominiquesydow opened this issue Jan 14, 2021 · 2 comments
Open

Implement serialization protocol in SwaggerClient class? #471

dominiquesydow opened this issue Jan 14, 2021 · 2 comments

Comments

@dominiquesydow
Copy link

Issue

Using bravado's SwaggerClient in a multiprocessing protocol causes a RecursionError.

Example

Code for minimal example:

from multiprocessing import Pool

from bravado.client import SwaggerClient

API_DEFINITIONS = "https://klifs.net/swagger/swagger.json"
CLIENT = SwaggerClient.from_url(API_DEFINITIONS, config={"validate_responses": False})

pool = Pool(processes=2)
type_list = pool.map(type, [CLIENT, CLIENT])
print(type_list)
pool.close()
pool.join()

Output:

Process ForkPoolWorker-2:
Traceback (most recent call last):
  File "/home/dominique/.local/miniconda/envs/kissim/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/home/dominique/.local/miniconda/envs/kissim/lib/python3.8/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/home/dominique/.local/miniconda/envs/kissim/lib/python3.8/multiprocessing/pool.py", line 114, in worker
    task = get()
  File "/home/dominique/.local/miniconda/envs/kissim/lib/python3.8/multiprocessing/queues.py", line 358, in get
    return _ForkingPickler.loads(res)
  File "/home/dominique/.local/miniconda/envs/kissim/lib/python3.8/site-packages/bravado/client.py", line 170, in __getattr__
    return self._get_resource(item)    
return self._get_resource(item)
  File "/home/dominique/.local/miniconda/envs/kissim/lib/python3.8/site-packages/bravado/client.py", line 148, in _get_resource
    resource = self.swagger_spec.resources.get(item)

  # Repeating the line 170 and 148 errors over and over again

RecursionError: maximum recursion depth exceeded

Potential solution

Add serialization protocol in SwaggerClient by defining __getstate__ and __setstate.

@jaimergp managed to solve the problem in our opencadd package by implementing such a serialization protocol in a SerializationSwaggerClient subclass. Maybe something like this could be added directly to the SwaggerClient class?

@macisamuele
Copy link
Collaborator

@dominiquesydow thanks for reporting the issue. It would be great if you could open a PR yourself especially considering that you have found a way to address the issue.

Having such change would allow us to pass the SwaggerClient into a process Pool but more in general would allow us to pickle/unpickle the client (which might time consuming for big swagger specs)

@dominiquesydow
Copy link
Author

Great, happy to do that within the next days!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants