Are there limitations on the length of strings passed to php by python in a service? I had a php service running that retrieved data from a python script. Worked well. I decided to add data from another python script.

I tried several ways and all failed. I retrieved each data and concatenated in the php script, before writing to a text file. I tried calling one python script from another python script, called in turn from the php service script, and concatenating the strings in the calling python script, thus sending a single string back to the php service. No matter what happens, or what strategy or configuration I use, the data from one or both python scripts is lost if I try to get both.

I finally combined the two python scripts into a single script. Now all the data is sent to php but lost before being written to file. I can run the files in almost any configuration from the command line, and all will work. Easily. The scripts also run well with setsid. But once they're placed in a service, the problems arise. The data returned are string representations of numbers, The readings from an analog-to-digital converter, and calculations of the current solar azimuth and elevation. As i said, before this, I could get one of the python sctipts data printed to file.

I get the impression I'm running into a length boundary of some kind.

The possible combinations of variables is so great, I decided to check the stack exchange before going further.

I run the scripts on a Raspberry Pi 3B+ with the Raspbian Stretch operating system from my Windows laptop using Mobaxterm in SSH sessions over the Raspberry Pi wifi access point or household wifi.

This is a sample of the solar controller's data:
10:17:00,10:17:00 AM,2018-06-29,46.64,4.21,196.77,25.39,7.75,196.77,24

The first four here are ADC reads, the last four, calculations:
1.827,1.7,1.697,1.949,285.6145,18.655,   0.679704001401382,  0.031167595280969528
A0,   A1, A2,   A3,   Azimuth, Elevat.,  Sm,                 Si

This data is written to file immediately after the controller data. All once every thirty seconds.

Briefly, the php script makes a call to python to collect data, then collects data from a charge controller. The controller data is formatted and written to a text file. At the end of the script, the python data is written after the controller data, to the same file and in csv format with a header, rows and columns. The python data form the last eight columns of the table. This is repeated every 30 seconds throughout the day.

Here are the files:

    /*   epoutX.php   */
    require_once 'PhpEpsolarTracer.php';
    $tracer = new PhpEpsolarTracer('/dev/ttyXRUSB0');
    $y = 1;
    $x = 3;
    while($x = 3):
    $myfile = fopen('/var/www/html/data_'.date('m-d-Y').'.txt', "a") or die("Unable to open file!"."\n");
    $pythonephemsensor1 = `/usr/bin/env python3.5 /var/www/html/resources/ephem-sensor.py`;
    $pythonephemsensor = preg_replace("/\r|\n/","",$pythonephemsensor1); 
    $nano = time_nanosleep(30, 0); // ( sec, nanoseconds[<1x10^9] )
      if ($tracer->getRealtimeData()) {
        $date=date("Y-m-d H:i:s");
        $seconds = strtotime($date);
        $seconds /= 30;
        $seconds = round($seconds);
        $seconds *= 30;
        $date2 = date("h:i:s", $seconds);
        $date = date("h:i:s A", $seconds);
        $today = date("Y-m-d"); # Y-m-d
        $now = $date;
        fwrite($myfile, $date2);
        fwrite($myfile, ",");
        fwrite($myfile, $now);
        fwrite($myfile, ",");
        fwrite($myfile, $today.",");
        while ($i<=5):
          $dat[$i] = $tracer->realtimeData[$i].",";
           fwrite($myfile, $dat[$i]);
        $dat[$i] = $tracer->realtimeData[$i].",";
        fwrite($myfile, $dat[$i]);
        fwrite($myfile, $pythonephemsensor.",\n");

The sensor read code was only eight lines, and was added to the calculations code. The sensor read code is at the top of the Python script:

    Python script:
    # ephem-sun
    import math
    import ephem
    import time
    from time import gmtime, strftime, localtime
    import Adafruit_ADS1x15

    # begin sensor reading
    adc01 = Adafruit_ADS1x15.ADS1115(address=0x49)
    GAIN = 1
    lav = 4.1/32767
    values = [0]*4
    for i in range(4):
     values[i] = round(adc01.read_adc(i, gain=GAIN, data_rate=128)*lav, 3)
    # end sensor reading

    # begin solar calculations
    home = ephem.Observer()
    home.lat, home.lon ='36.3401','-82.1950'
    radians = r
    sun = ephem.Sun()
    s_az1 = repr(sun.az*radians)
    s_alt1 = repr(sun.alt*radians)
    s_az = float(s_az1)
    s_alt = float(s_alt1)
    s_az = round(s_az,4)
    s_alt = round(s_alt,4)
    alpha=s_alt #40.5603 # Sel
    Z=90-alpha #49.4397 # 90-alpha
    theta=s_az #175.9508 # Saz
    s_az2 = str(s_az)
    s_alt2 = str(s_alt)
    Si2 = str(Si)
    Sm2 = str(Sm)
    # Print both data sets to PHP
    # end solar calculations

End of code.

  • 2
    Nobody can diagnose such a large block of code easily. Make a copy of your program and start deleting anything not related to the problem. Eventually, the problematic code will emerge. This is very very tedious. It also works. Then edit your answer with the problematic code only.
    – OyaMist
    Commented Jun 29, 2018 at 14:39
  • I've done that. The related code includes file read/write functions and string parsing functions and print/echo functions. The question of string lengths limitations remains.
    – Jim Julian
    Commented Jun 29, 2018 at 17:50
  • That is a good point, though. I described the simplified version: php writes, python writes.
    – Jim Julian
    Commented Jun 29, 2018 at 17:53
  • What happens when you delete a single field from your programs and try it? Does it still fail? If so, you haen't gotten to the minimum code needed to reproduce the bug.
    – NomadMaker
    Commented Jun 29, 2018 at 21:37
  • I don't think minimizing is going to replace understanding the problem. The program runs from the command line. Only as a service does the program fail. I think the key issue is complex numbers (n.real+n.imag) in the calculations. I've minimized the code with no effect. The writes are simple and separeate fromeverythjoing else, so minimalization is not really possible. The relevent code is already minimized. Python calls, write statements are the fundamental elements in this issue. Minimalization is for those who can't read the code. Anything else?
    – Jim Julian
    Commented Jun 30, 2018 at 2:24

1 Answer 1


I posted this before, but someone has apparently deleted the answer.

I ended up with a work-around.

I solved my problem but did not solve the PHP problem.

I'm accepting my answer so we can all move on. Not satisfying, but clean.

I wrote another service to save sensor data to a second data file. This works out since the amount of data I was accumulating was getting cumbersome in spreadsheet software and text editors. Isolation of sensor data makes archiving easier. This makes sense given I'm uisng the module model. I can add several more sensors or other devices that produce data and have each download data into a file specific to that device. PHP is a website software.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.