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?