Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Python Hangs When Using Class Object

  • 27-02-2018 3:08pm
    #1
    Registered Users, Registered Users 2 Posts: 1,987 ✭✭✭


    I'm having an issue with a Python script which is called from PHP via shell_exec() and the Python script hangs when called from PHP but the Python script is run via command line it runs and completes fine.

    Global.py
    import re
    from selenium import webdriver
    
    class Global:
    
        def getDriver(self):
            try:
                chrome_options = webdriver.ChromeOptions()
                chrome_options.add_argument('headless')
                chrome_options.add_argument('no-sandbox')
    
                # Windows - Dev Environment
                driver = webdriver.Chrome(executable_path='C:\chromedriver.exe', chrome_options=chrome_options)
                # Linux - Prod Environment
                #driver = webdriver.Chrome(chrome_options=chrome_options)
    
                return driver
            except Exception as e:
                print('Error: ' + str(e.args[0]))
    
        def closeDriver(self, driver):
            try:
                driver.close()
                return None
            except Exception as e:
                print('Error: ' + str(e.args[0]))
    

    script1.py
    import os, sys
    ROOT_DIR = os.path.normpath(os.path.join(os.path.abspath(__file__), '../..'))
    sys.path.insert(0, ROOT_DIR)
    from Global import Global
    
    globalObj = Global()
    
    try:
        # Load all items on page
        driver = globalObj.getDriver()
        driver.get(PARAMS[1])
    
        #... code to create a JSON file and populate it.
    
        # Close webdriver
        globalObj.closeDriver(driver)
    
        print('Completed Ref: ' + str(PARAMS[2]))
    
    except Exception as e:
        print('Error: ' + str(e.args[0]))
    

    There is very detailed info here: https://stackoverflow.com/questions/48988455/Python-script-hangs-when-called-via-php-shell-exec

    What I've found is that the class 'Global' I'm creating an object for and calling two functions, one to create a webdriver and pass the driver back and one that takes a driver as a parameter and closes it.

    If I remove the 'Global' class references and setup a webdriver within the current script, all will work fine so there is something I'm doing wrong or incorrectly with the Global class.

    Anyone ever seen or heard of anything like this?


Comments

  • Registered Users, Registered Users 2 Posts: 768 ✭✭✭14ned


    Ziycon wrote: »
    Anyone ever seen or heard of anything like this?

    Bugs like these are often from environment zomping by PHP to prevent various security exploits.

    Niall


  • Registered Users, Registered Users 2 Posts: 6,287 ✭✭✭Talisman


    The obvious answer is that the Global class is not being found when you call the script from PHP.


  • Registered Users, Registered Users 2 Posts: 1,987 ✭✭✭Ziycon


    @14ned, it was all working fine for the past 3 years up until i moved the web driver creation and closure out of the scripts and into a 'Global' class.

    @Talisman, the global class is being found as the JSON file the script is generating and populating is being created correctly, it just looks like the webdriver isn't closed out properly and hangs now.

    It's looking like its the chromedriver now, I changed the webdriver to Firefox and PhantomJS and all works fine, it looks like the chromedriver process is never closed in task manager once created.

    Hmmm, not sure where to go now.


Advertisement