You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@spark.apache.org by "M Mehrtens (Jira)" <ji...@apache.org> on 2022/02/08 21:07:00 UTC

[jira] [Updated] (SPARK-38143) Python UDFs freeze when libraries with non-standard imports are imported in the namespace

     [ https://issues.apache.org/jira/browse/SPARK-38143?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

M Mehrtens updated SPARK-38143:
-------------------------------
    Attachment: example.zip

> Python UDFs freeze when libraries with non-standard imports are imported in the namespace
> -----------------------------------------------------------------------------------------
>
>                 Key: SPARK-38143
>                 URL: https://issues.apache.org/jira/browse/SPARK-38143
>             Project: Spark
>          Issue Type: Bug
>          Components: PySpark
>    Affects Versions: 3.1.2, 3.2.0, 3.2.1
>            Reporter: M Mehrtens
>            Priority: Major
>         Attachments: example.zip
>
>
> When importing libraries with nontraditional imports (e.g. an `__init__.py` that doesn't just explicitly import its submodules, but instead uses a function or a custom loader) PySpark UDFs will freeze.
> Two libraries that demonstrate this behavior:
>  * [snscrape|https://github.com/JustAnotherArchivist/snscrape/blob/master/snscrape/modules/__init__.py]
>  * [transformers|https://github.com/huggingface/transformers/blob/master/src/transformers/__init__.py]
> The attached `example.zip` archive contains a module that demonstrates this behavior. To reproduce:
> ```bash
> $ unzip example.zip
> $ cd example
> $ pip install --editable .
> $ example
> ```
> This example module uses relative imports everywhere, with no circular imports.
> To demonstrate a fix to this issue, simply erase `import_stuff.py`:
> ```bash
> $ echo "print('erased')" > example/import_stuff.py
> ```
> Then rerun the example:
> ```bash
> $ example
> ```
> My hypothesis is that some libraries take a long time to import. transformers can take up to 4 seconds to import, and snscrape up to 1 second. I think this import performance is causing Spark execution of our UDFs to just slow down to an unusable level.
> *Note:* For some reason, changing the import behavior fixes the Spark performance issue, even though the import performance itself doesn't change. 
> So, it's some combination of "library that takes a long time to import" and "library with weird imports" that breaks Spark UDFs. I have no clue what Spark internals cause this to be true, but I can demonstrate it with the `snscrape` library with a small modification to their import behavior [demonstrated here|https://github.com/michaelcredera/snscrape/blob/feature/update-imports/snscrape/modules/__init__.py]. This fork of `snscrape` has no associated UDF performance issues, despite the time to import the library being constant.
> This is something we can avoid in our code by deferring the import of these "weird" libraries, but seems like odd behavior that I'd like to understand better. If anyone can help me get some insight into why this might be the case in Spark/PySpark, I would greatly appreciate it!



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@spark.apache.org
For additional commands, e-mail: issues-help@spark.apache.org