Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python script does not run with sleep in loop #821

Closed
Katafalkas opened this issue Nov 17, 2014 · 10 comments
Closed

python script does not run with sleep in loop #821

Katafalkas opened this issue Nov 17, 2014 · 10 comments

Comments

@Katafalkas
Copy link

Hey,

I am trying to run a python script under pm2. It would be like cron but would run more often.

import time

def main():
  while True:
    print time.sleep
    print "bar"
    time.sleep(1)

if __name__ == '__main__':
  print "Hello Script"
  main()

The problem is that for some reason if there is a sleep in while loop, or for loop - script does not even start. if I comment the time.sleep(1) - script runs fine:

# pm2 logs
...
[test.py-24 (out)] bar
[test.py-24 (out)] <built-in function sleep>
...

If I run script regulary with # python test.py the sleep works as intended.

Any ideas ?

@soyuka
Copy link
Collaborator

soyuka commented Nov 17, 2014

How are you running the python script?
Try pm2 start -x --interpreter=python test.py

@Katafalkas
Copy link
Author

Hey. Sorry, forgot to mention. I start the script with pm2 start /root/test.py -x --interpreter python.
Tried with pm2 start -x --interpreter=python test.py - outcome was the same.

@jlagneau
Copy link
Contributor

It should work without the interpreter option (since 7d3bd3f)

Try directly with pm2 start test.py.

There's already a test for an infinite loop in python in this commit : https://github.com/Unitech/PM2/blob/7d3bd3fabeabcfdf5c8b754f3f863c2cb1b01d46/test/fixtures/binary-py-file.py

I am trying to reproduce your bug but it works fine here.

@jlagneau
Copy link
Contributor

My pm2 logs after I started your script:

...
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
[test.py-0 (out)] <built-in function sleep>
[test.py-0 (out)] bar
...
% pm2 --version
0.12.1
% python --version
Python 2.7.6

@Katafalkas
Copy link
Author

Hey,
still not working:

# pm2 start test2.py
[PM2] Process test2.py launched
┌──────────┬────┬──────┬──────┬────────┬───────────┬────────┬─────────────┬─────────────┐
│ App name │ id │ mode │ PID  │ status │ restarted │ uptime │      memory │    watching │
├──────────┼────┼──────┼──────┼────────┼───────────┼────────┼─────────────┼─────────────┤
│  test.py │ 30 │ fork │ 9628 │ online │         0 │ 0s     │  4.770 MB   │ unactivated │
└──────────┴────┴──────┴──────┴────────┴───────────┴────────┴─────────────┴─────────────┘

# pm2 logs
[PM2] Starting execution sequence in -fork mode- for app name:test1.py id:30
[PM2] App name:test.py id:30 online

and there are no print statements.

pm2 --version
0.11.1

Gonna try to upgrade to new version of pm2

@Katafalkas
Copy link
Author

Tried with pm2 12.1 - still no changes. Logs are empty.

root@api:~# pm2 --version
0.12.1
root@api:~# python --version
Python 2.7.6
root@api:~# pm2 start test.py 
[PM2] Process test.py launched
┌──────────┬────┬──────┬──────┬────────┬───────────┬────────┬────────────┬──────────┐
│ App name │ id │ mode │ PID  │ status │ restarted │ uptime │     memory │ watching │
├──────────┼────┼──────┼──────┼────────┼───────────┼────────┼────────────┼──────────┤
│ test.py  │ 4  │ fork │ 9967 │ online │         0 │ 0s     │ 4.773 MB   │ disabled │
└──────────┴────┴──────┴──────┴────────┴───────────┴────────┴────────────┴──────────┘
 Use `pm2 info <id|name>` to get more details about an app
root@api:~# pm2 logs
########### Starting streaming logs for [all] process
[PM2] Starting execution sequence in -fork mode- for app name:test.py id:4
[PM2] App name:test.py id:4 online

@Katafalkas
Copy link
Author

So i came back and turned logs back on - seems that the script is actually working, but the logs are really lagging. After I start the script the logs are empty for quite few minutes and the pm2 just dumps a bunch of logs into the log file.

@soyuka
Copy link
Collaborator

soyuka commented Nov 17, 2014

Might be #822 (comment) related?

@Katafalkas
Copy link
Author

Could be. I ll keep an eye on it.

@soyuka
Copy link
Collaborator

soyuka commented Nov 17, 2014

Closing for now ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants