Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Makes @st.cache_resource compatible with Cython #10353

@tutu-sol

Description

@tutu-sol

Checklist

  • I have searched the existing issues for similar feature requests.
  • I added a descriptive title and summary to this issue.

Summary

For a user program that uses @st.cache_resource, currently it is not possible to use Cython to compile the user program.

As in cache_utils, streamlit uses inspect.getsource(func) to attempt to read the source, Cython compiled code will cause an TypeError exception but this is not handled by the fallback to bytecode.

Why?

No response

How?

Instead of handling OSError, fallback to bytecode upon any Exception would fix the issue.

Additional Context

  File "<redacted>/site-packages/streamlit/runtime/caching/cache_resource_api.py", line 238, in __call__
    return self._decorator(
  File "<redacted>/site-packages/streamlit/runtime/metrics_util.py", line 409, in wrapped_func
    result = non_optional_func(*args, **kwargs)
  File "<redacted>/site-packages/streamlit/runtime/caching/cache_resource_api.py", line 431, in _decorator
    return make_cached_func_wrapper(
  File "<redacted>/site-packages/streamlit/runtime/caching/cache_utils.py", line 161, in make_cached_func_wrapper
    cached_func = CachedFunc(info)
  File "<redacted>/site-packages/streamlit/runtime/caching/cache_utils.py", line 193, in __init__
    self._function_key = _make_function_key(info.cache_type, info.func)
  File "<redacted>/site-packages/streamlit/runtime/caching/cache_utils.py", line 488, in _make_function_key
    source_code = inspect.getsource(func)
  File "/usr/lib/python3.10/inspect.py", line 1139, in getsource
    lines, lnum = getsourcelines(object)
  File "/usr/lib/python3.10/inspect.py", line 1121, in getsourcelines
    lines, lnum = findsource(object)
  File "/usr/lib/python3.10/inspect.py", line 940, in findsource
    file = getsourcefile(object)
  File "/usr/lib/python3.10/inspect.py", line 817, in getsourcefile
    filename = getfile(object)
  File "/usr/lib/python3.10/inspect.py", line 797, in getfile
    raise TypeError('module, class, method, function, traceback, frame, or '
TypeError: module, class, method, function, traceback, frame, or code object was expected, got cython_function_or_method

This trace was collected with streamlit==1.41.0 but the issue persists with the current branch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature:cacheRelated to `st.cache_data` and `st.cache_resource`type:enhancementRequests for feature enhancements or new features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions