Standalone python script to retrieve django models - python

I have been figuring on how to run a script on background, so that i can process an object without having the user to wait for the server to respond.
Views.py
import subprocess, sys
def allocate_request(request, event_id):
event = get_object_or_404(MeetingEvent, id=event_id)
subprocess.Popen([sys.executable, 'sorting.py', str(event_id)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
return HttpResponse("Request Submitted.")
and in the same directory, Sorting.py
import sys
class Organize:
if len(sys.argv) <= 1:
event_id = int(sys.argv[1])
event_i = MeetingEvent.objects.get(id=event_id)
Problem is...
the script itself can't be run alone,
from .models import MeetingEvent
ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package
Would appreciate any help :)

Related

call_command not working, did everything like the tutorial

So just like the title suggest. i followed the instrcutions.
this is the code.
the command doesnt work not from the command line either. zero idea why
import time
from django.db import connections
from django.db.utils import OperationalError
from core.management.base import BaseCommand
class Command(BaseCommand):
"""django command to pause execution is available"""
def handle(self, *args, **options):
self.std.write('waiting for database...')
db_conn = None
while not db_conn:
try:
db_conn = connections[default]
except:
self.std.out('Database unavailable, wait 1 sec')
time.sleep(1)
self.std.write(self.style.SUCCESS('Database available'))
in the call is in a unit test just a line with command_call('wait_for_db') (the name of the file ofcourse)
this is the test unit and this is the wait for db command
thnkx yall
Management commands for your app should be in a directory named <app>/management/commands/. You have named the management directory "management.py", it should be "management"

Running nosetest corrupts putty session

When I run nosetests in my putty session, the command prompts stops working. For example, what ever key I type turns into )
The only way I found so far to recover is to restart the session.
The command I run is:
nosetests -v --with-xunitmp -m "(?:\b|_)[Tt]est" --xunitmp-file nosetests.xml --processes=10 --process-timeout=600
I use nosetests 1.3.7 and python 3.5.1
Edit:
I've narrowed it down a bit.
It is happening outside of tmux (in a putty session)
It is happening because I start other processes from my python tests
Here's an example:
from unittest import TestCase
from subprocess import Popen
import time
class MyTest(TestCase):
def test_this(self):
self.assertTrue(True)
def test_with_process(self):
process = Popen(['watch', 'ls'])
time.sleep(1)
if process.poll() is None:
process.kill()
Edit 2:
It seems like redirecting the subprocess to /dev/null fixes the issue:
from unittest import TestCase
from subprocess import Popen, DEVNULL
import time
class MyTest(TestCase):
def test_this(self):
self.assertTrue(True)
def test_with_process(self):
process = Popen(['watch', 'ls'],
stdout=DEVNULL,
stderr=DEVNULL,
stdin=DEVNULL)
time.sleep(1)
if process.poll() is not None:
print("KILLING")
process.kill()
process.communicate()
It resolves the issue, I'd like to understand why this is happening...

python import another python script not working

I have two python scripts which are in the same directory(C:\\Users\\user1\\general). I want to execute a function in one script from the 2nd second script and so I am trying to import script1 in script2. Here is my script2:
import sys
sys.path.insert(0, 'C:\\Users\\user1\\general')
import script1
from flask import Flask
from flask_socketio import SocketIO, send
app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecret'
socketio = SocketIO(app)
#socketio.on('message')
def handleMessage(msg):
print('Message: ' + msg)
# script1 has function called api_call(id,message)
messsage = api_call('user1', msg)
send(messsage, broadcast=True)
if __name__ == '__main__':
socketio.run(app)
Here is my script1:
import sys
def api_call(Id,message):
# Code processing
if __name__ == '__main__':
uId = sys.argv[0]
message = sys.argv[1]
api_call(userId,message)
When I execute above script2 I get NameError: name 'api_call' is not defined. It seems somehow the script1 is not getting imported and so the function is not getting through.
Note: Earlier I had tried without using sys.path.insert() and same outcome then also.
Try from script1 import api_call: this would allow to import from script1 module the api_call function.
Python 2
Create __init__.py in the same folder.
Import using following command
from script1 import api_call
Python 3
Import using following command
from .script1 import api_call

Python program as Windows Service

below is my code that I am trying to turn into a windows service. You'll see test.py as the call it makes and all this is a short script that writes into a log file (as a test).
The code is there to make it a windows service and it does that fine, but when I run it nothing writes into the log file. Help greatly appreciated. Below is the code:
import win32service
import win32serviceutil
import win32api
import win32con
import win32event
import win32evtlogutil
import os, sys, string, time
class aservice(win32serviceutil.ServiceFramework):
_svc_name_ = "MyServiceShortName"
_svc_display_name_ = "A python test"
_svc_description_ = "Writing to a log"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
import servicemanager
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))
self.timeout = 1000 #1 seconds
# This is how long the service will wait to run / refresh itself (see script below)
while 1:
# Wait for service stop signal, if I timeout, loop again
rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
# Check to see if self.hWaitStop happened
if rc == win32event.WAIT_OBJECT_0:
# Stop signal encountered
servicemanager.LogInfoMsg("SomeShortNameVersion - STOPPED!") #For Event Log
break
else:
#what to run
try:
file_path = "test.py"
execfile(file_path)
except:
pass
#end of what to run
def ctrlHandler(ctrlType):
return True
if __name__ == '__main__':
win32api.SetConsoleCtrlHandler(ctrlHandler, True)
win32serviceutil.HandleCommandLine(aservice)
Edit: Thought for the sake of it I'd include my test.py file's code, it has unnecessary imports but will get the job done if you run it alone.
import win32service
import win32serviceutil
import win32api
import win32con
import win32event
import win32evtlogutil
import os
logfile = open("log.txt", "a") #open file to log restart timestamp
logfile.write("\nthat's good!!!!")
logfile.close()
Okay so I figured it out and would like to come back and post in case anyone else is dealing with this, all though this was a tad unique.
You have to specify your file path if you are within a windows service, duh... but it wasn't done and I pulled my hair out for no reason.
file_path = "test.py"
should have been
file_path = r"c:\users\...\test.py"
Be careful when using '\' for windows file paths. They must be escaped like '\\' or the string must be declared as raw string ('r'). Using the unix like slash '/' separators works also but may look odd for windows users.

How to use FIFO to communitace between python subprocesss

I try to use Python (2.6) subprocess module to communicated between two external programs. When I started to pump more data 'client' stopped to received everything. I thought it might be related to limited size of subprocess.PIPE object (http://thraxil.org/users/anders/posts/2008/03/13/Subprocess-Hanging-PIPE-is-your-enemy/). So I decided to try fifo, but it didn't worked as expected. How should I use fifo to communicated between two external program called from python script. Currently my script stops at (waiting for another end of fifo):
pipe_name = "stream-%s.fifo"%self.transponder
os.mkfifo(pipe_name)
self.stream = Popen(program1, stdout=open(pipe_name,'w'),
stderr=open("test.log",'w'))
I didn't manage to get mkfifo working. However I managed to have server/client communicate via os.pipe:
#!/usr/bin/env python
from subprocess import Popen
import os
from itertools import cycle
from time import sleep
from sys import stdout, executable
def client():
for letter in cycle('ABCDE'):
stdout.write(letter)
stdout.flush()
sleep(1)
def main(argv=None):
import sys
from argparse import ArgumentParser
argv = argv or sys.argv
parser = ArgumentParser()
parser.add_argument('--client', default=False, action='store_true')
args = parser.parse_args(argv[1:])
if args.client:
client()
r, w = os.pipe()
pipe = Popen([executable, __file__, '--client'], stdout=os.fdopen(w, 'w'))
try:
client_out = os.fdopen(r, 'r')
while True:
letter = client_out.read(1)
print(letter)
except KeyboardInterrupt:
pipe.kill()
if __name__ == '__main__':
main()

Categories

Resources