<?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: What is the alternative for sys.exit(0) in Databricks in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/what-is-the-alternative-for-sys-exit-0-in-databricks/m-p/38520#M609</link>
    <description>&lt;P&gt;I tested with simple code and it worked because return statements only work inside the functions and converting all the code into the main function makes it work. I will test the same with a full scale of code and if that works then I will mark this as the solution.&lt;/P&gt;</description>
    <pubDate>Wed, 26 Jul 2023 16:27:59 GMT</pubDate>
    <dc:creator>Ramana</dc:creator>
    <dc:date>2023-07-26T16:27:59Z</dc:date>
    <item>
      <title>What is the alternative for sys.exit(0) in Databricks</title>
      <link>https://community.databricks.com/t5/get-started-discussions/what-is-the-alternative-for-sys-exit-0-in-databricks/m-p/38301#M588</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;We are working on a migration project from Cloudera to Databricks.&lt;/P&gt;&lt;P&gt;All our code is in .py files and we decided to keep the same in Databricks as well and try to execute the same from GIT through Databricks workflows.&lt;/P&gt;&lt;P&gt;We have two kinds of exit functionality requirements:&lt;/P&gt;&lt;P&gt;1. Soft exit with sys.exit(0)- when the job criteria meet some condition, then we do soft exist with sys.exit(0), which basically terminates the job softly by marking the job as successful&lt;/P&gt;&lt;P&gt;2. Job Terminate with sys.exit(1) -&amp;nbsp;when the job criteria meet some condition, then we do terminate the job with sys.exit(1), which basically terminates the job by marking the job as failed.&lt;/P&gt;&lt;P&gt;the above criteria work as expected in any Python environment but not in Databricks.&lt;/P&gt;&lt;P&gt;In Databricks, both sys.exit(1) as well as sys.exit(0) are marked as Job failed.&lt;/P&gt;&lt;P&gt;I read an article in Community and somebody mentioned that "&lt;SPAN&gt;Usage of spark.stop(), sc.stop() , System.exit() in your application can cause this behavior. Databricks manages the context shutdown on its own. Forcefully closing it can cause this abrupt behavior.&lt;/SPAN&gt;"&lt;/P&gt;&lt;P&gt;Link:&amp;nbsp;&lt;A href="https://community.databricks.com/t5/data-engineering/why-do-i-see-my-job-marked-as-failed-on-the-databricks-jobs-ui/m-p/20868" target="_blank"&gt;https://community.databricks.com/t5/data-engineering/why-do-i-see-my-job-marked-as-failed-on-the-databricks-jobs-ui/m-p/20868&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;If this is true, then what is the best alternative to achieve sys.exit(0) in Databricks?&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Any help would greatly be appreciated.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;FYI:&lt;/STRONG&gt; we have code in .py files and we don't want to use the Notebooks for PROD jobs.&lt;/P&gt;&lt;P&gt;Here is an example to execute and see the behavior:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;import&lt;/SPAN&gt;&lt;SPAN&gt; sys&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;bucket_name = &lt;/SPAN&gt;&lt;SPAN&gt;"prod"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;if&lt;/SPAN&gt;&lt;SPAN&gt; bucket_name == &lt;/SPAN&gt;&lt;SPAN&gt;"prod"&lt;/SPAN&gt;&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"Success. It is PROD. Existing with 0"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt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PAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"Fail. It is DEV. Existing with 1"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; sys.exit(&lt;/SPAN&gt;&lt;SPAN&gt;1&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;print&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"outside if else"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2023 22:07:32 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/what-is-the-alternative-for-sys-exit-0-in-databricks/m-p/38301#M588</guid>
      <dc:creator>Ramana</dc:creator>
      <dc:date>2023-07-24T22:07:32Z</dc:date>
    </item>
    <item>
      <title>Re: What is the alternative for sys.exit(0) in Databricks</title>
      <link>https://community.databricks.com/t5/get-started-discussions/what-is-the-alternative-for-sys-exit-0-in-databricks/m-p/38513#M608</link>
      <description>&lt;P&gt;Ramana,&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I checked internally and the suggestion is to structure your code in such a way that it returns from the main function when a certain condition is met. I modified your code to what you see below.&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# sys.exit(0) equivalent
def main():
    bucket_name = "prod"
    if bucket_name ==  "prod":
        return
    # Rest of your code

if __name__ == "__main__":
    main()&lt;/LI-CODE&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I ran your original code and the job showed as failed in Databricks. Then I ran the modified function that returns from main and the job showed as succeeded. I believe this is the best alternative to achieve your expected sys.exit(0) behavior in Databricks.&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Wed, 26 Jul 2023 15:34:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/what-is-the-alternative-for-sys-exit-0-in-databricks/m-p/38513#M608</guid>
      <dc:creator>AndrewN</dc:creator>
      <dc:date>2023-07-26T15:34:56Z</dc:date>
    </item>
    <item>
      <title>Re: What is the alternative for sys.exit(0) in Databricks</title>
      <link>https://community.databricks.com/t5/get-started-discussions/what-is-the-alternative-for-sys-exit-0-in-databricks/m-p/38520#M609</link>
      <description>&lt;P&gt;I tested with simple code and it worked because return statements only work inside the functions and converting all the code into the main function makes it work. I will test the same with a full scale of code and if that works then I will mark this as the solution.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2023 16:27:59 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/what-is-the-alternative-for-sys-exit-0-in-databricks/m-p/38520#M609</guid>
      <dc:creator>Ramana</dc:creator>
      <dc:date>2023-07-26T16:27:59Z</dc:date>
    </item>
    <item>
      <title>Re: What is the alternative for sys.exit(0) in Databricks</title>
      <link>https://community.databricks.com/t5/get-started-discussions/what-is-the-alternative-for-sys-exit-0-in-databricks/m-p/38584#M615</link>
      <description>&lt;P&gt;I tested with different levels of nesting and it is working as expected.&lt;/P&gt;&lt;P&gt;Here is the sample code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sys
bucket_name =  "prod"# str(sys.argv[1]).lower()

def main():
    i,j=0,0
    while j&amp;lt;=2:
        print(f"while loop iteration: {j}")
        for i in range(0,3):
            print(f"for loop iteratation: {i}")
            if bucket_name ==  "prod" and j==1 and i==1:
                print("Success. It is PROD. Existing with 0")
                return True
                print("After return")
            # Rest of your code
            else:
                # print("Fail. It is DEV. Existing with 1")
                # sys.exit(1)
                print(f"Else: while iteration: {j} and for iteration: {i}")
                continue
            print("outside if else")
            i+=1
            print("inside for loop")
        j+=1
        print("end of for loop")
    print("end of while loop")

if __name__ == "__main__":
    main()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 14:32:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/what-is-the-alternative-for-sys-exit-0-in-databricks/m-p/38584#M615</guid>
      <dc:creator>Ramana</dc:creator>
      <dc:date>2023-07-27T14:32:10Z</dc:date>
    </item>
  </channel>
</rss>

