<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Serverless - can't parallelize UDF in applyInPandas in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/serverless-can-t-parallelize-udf-in-applyinpandas/m-p/135199#M50302</link>
    <description>&lt;P&gt;I was wrong in interpreting the results.&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;threading.&lt;/SPAN&gt;&lt;SPAN&gt;get_native_id&lt;/SPAN&gt;&lt;SPAN&gt;() does not work on serverless as on classic, so different threads return the same ID.&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;The time it takes to execute the test is obviously less than 40 seconds, if it was running on a single thread.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Fri, 17 Oct 2025 06:11:33 GMT</pubDate>
    <dc:creator>Dimitry</dc:creator>
    <dc:date>2025-10-17T06:11:33Z</dc:date>
    <item>
      <title>Serverless - can't parallelize UDF in applyInPandas</title>
      <link>https://community.databricks.com/t5/data-engineering/serverless-can-t-parallelize-udf-in-applyinpandas/m-p/135197#M50301</link>
      <description>&lt;P&gt;HI all&lt;/P&gt;&lt;P&gt;Serverless V3 solved an error of mismatching python versions between driver and worker which I had on V2 (can't remember the exact wording).&lt;/P&gt;&lt;P&gt;So I'd been running this on classic compute so far.&lt;/P&gt;&lt;P&gt;Today I tried on serverless to a partial success -&lt;SPAN&gt;&amp;nbsp;unfortunately the &lt;/SPAN&gt;&lt;STRONG&gt;UDF is being executed on a single CPU / thread&lt;/STRONG&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;I use &lt;STRONG&gt;repartition&lt;/STRONG&gt; to split the workload to worker nodes.&lt;/P&gt;&lt;P&gt;Here is a mock script to demonstrate how I execute UDF. In the mock UDF just sleeps a second:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import pandas as pd
from datetime import datetime
from time import sleep
import threading

# test function
def func(x: pd.DataFrame):
    sleep(1)
    return pd.DataFrame({'id': x['id'], 'timestamp': str(datetime.now()), 'thread': threading.get_native_id()})

# from pandas
pdf = pd.DataFrame({'id': range(40)})
sdf = spark.createDataFrame(pdf)

now = datetime.now()
sdf = sdf.repartition(8, "id").groupby('id').applyInPandas(func, schema="id int, timestamp string, thread int")
result = spark.createDataFrame(sdf.toPandas()) # trigger lazy evaluation
print((datetime.now() - now).total_seconds())

# expected at least 4
display(result.groupBy("thread").count())&lt;/LI-CODE&gt;&lt;P&gt;It repartitions an array 40 into 8, but as classic compute has only 4 CPU, it will execute on 4 threads:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Dimitry_1-1760679790069.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/20798iD93CE83D13D2C8B9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Dimitry_1-1760679790069.png" alt="Dimitry_1-1760679790069.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Now, when the same runs on serverless, I'm getting a single thread:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Dimitry_2-1760679824765.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/20799i5E56CD9E47744580/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Dimitry_2-1760679824765.png" alt="Dimitry_2-1760679824765.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;QUESTION: how to parallelize job on serverless compute for my data frame, besides repartitioning (which obviously does not work)?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Oct 2025 06:05:40 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/serverless-can-t-parallelize-udf-in-applyinpandas/m-p/135197#M50301</guid>
      <dc:creator>Dimitry</dc:creator>
      <dc:date>2025-10-17T06:05:40Z</dc:date>
    </item>
    <item>
      <title>Re: Serverless - can't parallelize UDF in applyInPandas</title>
      <link>https://community.databricks.com/t5/data-engineering/serverless-can-t-parallelize-udf-in-applyinpandas/m-p/135199#M50302</link>
      <description>&lt;P&gt;I was wrong in interpreting the results.&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;threading.&lt;/SPAN&gt;&lt;SPAN&gt;get_native_id&lt;/SPAN&gt;&lt;SPAN&gt;() does not work on serverless as on classic, so different threads return the same ID.&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;The time it takes to execute the test is obviously less than 40 seconds, if it was running on a single thread.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 17 Oct 2025 06:11:33 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/serverless-can-t-parallelize-udf-in-applyinpandas/m-p/135199#M50302</guid>
      <dc:creator>Dimitry</dc:creator>
      <dc:date>2025-10-17T06:11:33Z</dc:date>
    </item>
  </channel>
</rss>

