473,890 Members | 5,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows service and pyc files


Hello,

I have a win32 service written in Python that starts a plain
application, written in Python.

The win32 service tries to launch the application in a while loop and
logs the return value of the os.system call. That's all.

The application is a simple Python program that connects to an https
xml/rpc server, and works with the data retrieved from that server. It
is written as an application because it must be able to download updates
for itself. Here is how it works:

a.) connect to server
b.) compare current version with the latest
c.) if there is a difference, then download all sources from the server,
delete all pyc files and exit; otherwise start processing

I could not write a self-restarting server, and I did not want to
write/install two services for one problem. The win32 service is very
small and primitive so probably I will not need to update it. I think
the basic idea is good, but...

When there is a client update available, my application updates itself
cleanly and exists. Then the service tries to restart the application,
but it cannot. os.system returns with OS error code -1. The pyc files
are NOT generated for the application. However, if I start the
application from the command line, then pyc files are created, and then
the service will also start the application immediatelly. The win32
service is running as "Localsyste m" so it is sure that it has write
permission on all files.

I cannot log out the error from the application since it is not started.
The only error message I have is OSError -1, but it tells me nothing
about the nature of the error.

Thanks,

Laszlo

Apr 4 '07 #1
7 2668
On Apr 4, 10:48 am, Laszlo Nagy <gand...@design aproduct.bizwro te:
Hello,

I have a win32 service written in Python that starts a plain
application, written in Python.

The win32 service tries to launch the application in a while loop and
logs the return value of the os.system call. That's all.

The application is a simple Python program that connects to an https
xml/rpc server, and works with the data retrieved from that server. It
is written as an application because it must be able to download updates
for itself. Here is how it works:

a.) connect to server
b.) compare current version with the latest
c.) if there is a difference, then download all sources from the server,
delete all pyc files and exit; otherwise start processing

I could not write a self-restarting server, and I did not want to
write/install two services for one problem. The win32 service is very
small and primitive so probably I will not need to update it. I think
the basic idea is good, but...

When there is a client update available, my application updates itself
cleanly and exists. Then the service tries to restart the application,
but it cannot. os.system returns with OS error code -1. The pyc files
are NOT generated for the application. However, if I start the
application from the command line, then pyc files are created, and then
the service will also start the application immediatelly. The win32
service is running as "Localsyste m" so it is sure that it has write
permission on all files.

I cannot log out the error from the application since it is not started.
The only error message I have is OSError -1, but it tells me nothing
about the nature of the error.

Thanks,

Laszlo
Have you tried the subprocess module for this rather than os.system?
You might be able to pipe errors to a file with it. You might be able
to use the traceback module for more verbose error catching too.

Mike

Apr 4 '07 #2
>
Have you tried the subprocess module for this rather than os.system?
You might be able to pipe errors to a file with it. You might be able
to use the traceback module for more verbose error catching too.
Okay, I'm tried this instead of os.system:

def dumpexc(e):
import sys,traceback,S tringIO
f = StringIO.String IO('')
ei = sys.exc_info()
traceback.print _exception(ei[0],ei[1],ei[2],file=f)
return f.getvalue()

def spawn():
# ... setup mydir here....
os.chdir(mydir)
prog = os.path.join(my dir,"Applicatio n.py")
params = [sys.executable, prog]
logger.info("Sp awing %s",str(params) )
fout = file(os.path.jo in(mydir,'error log.txt'),'wb+' )
try:
p = subprocess.Pope n(params, bufsize=1, stdout=fout.fil eno(),
stderr=fout.fil eno())
except Exception, e:
logger.error(du mpexc(e))
return -1
retcode = p.wait()
logger.info("Su bprocess exited, return code: %d",retcode)
fout.close()
return retcode

When I call spawn() from a service, this is written into the logfile:

2007-04-05 17:52:53,828 INFO .Spawner Spawing
['C:\\Python25\\ lib\\site-packages\\win32 \\PythonService .exe',
'T:\\Python\\Pr ojects\\Test\\A pplication.py']
2007-04-05 17:52:53,828 ERROR .Spawner Traceback (most recent call last):
File "T:\Python\Proj ects\Test\Proce ssor.py", line 40, in spawn_downloade r
p = subprocess.Pope n(params, bufsize=1, stdout=fout.fil eno(),
stderr=fout.fil eno())
File "C:\Python25\li b\subprocess.py ", line 586, in __init__
errread, errwrite) = self._get_handl es(stdin, stdout, stderr)
File "C:\Python25\li b\subprocess.py ", line 681, in _get_handles
p2cread = self._make_inhe ritable(p2cread )
File "C:\Python25\li b\subprocess.py ", line 722, in _make_inheritab le
DUPLICATE_SAME_ ACCESS)
TypeError: an integer is required
errorlog.txt is - of course - becomes an empty file.

When I call spawn() from an application, it works just fine. Any ideas?

Thanks,

Laszlo

Apr 5 '07 #3
When I call spawn() from a service, this is written into the logfile:

2007-04-05 17:52:53,828 INFO .Spawner Spawing
['C:\\Python25\\ lib\\site-packages\\win32 \\PythonService .exe',
'T:\\Python\\Pr ojects\\Test\\A pplication.py']
2007-04-05 17:52:53,828 ERROR .Spawner Traceback (most recent call last):
File "T:\Python\Proj ects\Test\Proce ssor.py", line 40, in spawn_downloade r
p = subprocess.Pope n(params, bufsize=1, stdout=fout.fil eno(),
stderr=fout.fil eno())
File "C:\Python25\li b\subprocess.py ", line 586, in __init__
errread, errwrite) = self._get_handl es(stdin, stdout, stderr)
File "C:\Python25\li b\subprocess.py ", line 681, in _get_handles
p2cread = self._make_inhe ritable(p2cread )
File "C:\Python25\li b\subprocess.py ", line 722, in _make_inheritab le
DUPLICATE_SAME_ ACCESS)
TypeError: an integer is required

Okay, here is what I learnt:

1. subprocess.Pope n cannot redirect stderr and stdout when called from a
win32 service. This is not documented, and makes debugging almost
impossible.
2. sys.executable becomes "pythonservice. exe" inside a win32 service.

If I specify r"C:\Python25\p ython.exe" instead of sys.executable, and
if I do not specify stdout and stderr parameters for subprocess.Pope n,
then my program starts to work. Here arises the question: how can I
find r"C:\Python25\p ython.exe" from inside a win32 service? Can I use this:

interpreter = os.path.join( os.path.split(s ys.executable),[0],
os.sep,os.sep,o s.sep,'Python.e xe' )

Is it safe? Please advise.
Thanks,

Laszlo
Apr 5 '07 #4
En Thu, 05 Apr 2007 13:00:52 -0300, Laszlo Nagy
<ga*****@design aproduct.bizesc ribió:
p = subprocess.Pope n(params, bufsize=1, stdout=fout.fil eno(),
stderr=fout.fil eno())

When I call spawn() from a service, this is written into the logfile:

2007-04-05 17:52:53,828 INFO .Spawner Spawing
['C:\\Python25\\ lib\\site-packages\\win32 \\PythonService .exe',
'T:\\Python\\Pr ojects\\Test\\A pplication.py']
2007-04-05 17:52:53,828 ERROR .Spawner Traceback (most recent call last):
File "T:\Python\Proj ects\Test\Proce ssor.py", line 40, in spawn_downloade r
p = subprocess.Pope n(params, bufsize=1, stdout=fout.fil eno(),
stderr=fout.fil eno())
File "C:\Python25\li b\subprocess.py ", line 586, in __init__
errread, errwrite) = self._get_handl es(stdin, stdout, stderr)
File "C:\Python25\li b\subprocess.py ", line 681, in _get_handles
p2cread = self._make_inhe ritable(p2cread )
File "C:\Python25\li b\subprocess.py ", line 722, in _make_inheritab le
DUPLICATE_SAME_ ACCESS)
TypeError: an integer is required
With a bit of guessing, I think I've found what's happening.
Since you don't provide a value for stdin, None is used. Inside
subprocess.py, method _get_handles, line 670, GetStdHandle *may* return
None; in that case _make_inheritab le fails.
If you print the value of p2cread in line 670 I bet you'll get None.
The fix is to test for None in _make_inheritab le (line 720):

if handle is not None:
return DuplicateHandle (...)

(else return None, implicit)
When I call spawn() from an application, it works just fine. Any ideas?
According to http://msdn2.microsoft.com/en-us/library/ms683231.aspx
GetStdHandle may return NULL (translated to None in Python) when invoked
from a service with no redirected standard handles. From an application,
there is no problem.

Please try the simple fix above to comfirm it works; I'll submit a patch
if that's the case.

--
Gabriel Genellina

Apr 5 '07 #5
En Thu, 05 Apr 2007 13:50:16 -0300, Laszlo Nagy
<ga*****@design aproduct.bizesc ribió:
1. subprocess.Pope n cannot redirect stderr and stdout when called from a
win32 service. This is not documented, and makes debugging almost
impossible.
Without the patch menctioned in my previous message, you must redirect all
stdin, stdout AND stderr (because the child cannot inherit the handles
from the parent service, as a service has no standard handles assigned
usually) or none of them.
2. sys.executable becomes "pythonservice. exe" inside a win32 service.

If I specify r"C:\Python25\p ython.exe" instead of sys.executable, and
if I do not specify stdout and stderr parameters for subprocess.Pope n,
then my program starts to work. Here arises the question: how can I
find r"C:\Python25\p ython.exe" from inside a win32 service? Can I use
this:

interpreter = os.path.join( os.path.split(s ys.executable),[0],
os.sep,os.sep,o s.sep,'Python.e xe' )
I think you meant to write: os.path.join(os .path.split(sys .executable)[0],
os.pardir, os.pardir, os.pardir, 'python.exe')

pythonservice.e xe is so Windows-specific that using os.pardir only makes
the code harder to read.
Anyway I'd use os.path.join(sy s.prefix, 'python.exe') (sys.prefix would be
C:\Python25 in your case)

--
Gabriel Genellina

Apr 5 '07 #6
>1. subprocess.Pope n cannot redirect stderr and stdout when called from a
win32 service. This is not documented, and makes debugging almost
impossible.

Without the patch menctioned in my previous message, you must redirect all
stdin, stdout AND stderr (because the child cannot inherit the handles
from the parent service, as a service has no standard handles assigned
usually) or none of them.
The truth is that subprocess.Pope n raises an exception when I try to
redirect stdin,stdout or stderr. If I do not redirect any of them, then
everything works fine for me. (Well, of course standard output is lost.)
>interpreter = os.path.join( os.path.split(s ys.executable),[0],
os.sep,os.sep, os.sep,'Python. exe' )

I think you meant to write: os.path.join(os .path.split(sys .executable)[0],
os.pardir, os.pardir, os.pardir, 'python.exe')
Yes, sorry.
pythonservice.e xe is so Windows-specific that using os.pardir only makes
the code harder to read.
Anyway I'd use os.path.join(sy s.prefix, 'python.exe') (sys.prefix would be
C:\Python25 in your case)
Oh, I did not know about that variable. Thanks! :-)

Best,

Laszlo

Apr 5 '07 #7
With a bit of guessing, I think I've found what's happening.
Since you don't provide a value for stdin, None is used. Inside
subprocess.py, method _get_handles, line 670, GetStdHandle *may* return
None; in that case _make_inheritab le fails.
If you print the value of p2cread in line 670 I bet you'll get None.
The fix is to test for None in _make_inheritab le (line 720):

if handle is not None:
return DuplicateHandle (...)

(else return None, implicit)

>When I call spawn() from an application, it works just fine. Any ideas?

According to http://msdn2.microsoft.com/en-us/library/ms683231.aspx
GetStdHandle may return NULL (translated to None in Python) when invoked
from a service with no redirected standard handles. From an application,
there is no problem.

Please try the simple fix above to comfirm it works; I'll submit a patch
if that's the case.
Yes, it fixed the problem. Please submit the patch. Thank you!

Laszlo

Apr 5 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
15241
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My employer has provided me with the SQL Server 2000 Personal disk from the SQL Server 2000 Enterprise kit as this is reported here on the MSDN web site to be the version that is supported on Windows XP. In fact so many of you kind people confess to having succeeded in doing it. I have tried...
3
5145
by: belgiozen | last post by:
Hi, I have a working windows service,it is looking for files on the disk and when some of the files are cupdated it calls an executable. But it takes a lot of time(about 10 minutes) to run the executable.So while that executable is running,if I try to stop the windows service while the executable(MYPROGRAM.EXE) is running,it will wait for a time(about 20 seconds) for the main windows thread to abort.If it does not finish its work, the...
10
4468
by: dermot | last post by:
I have wrriten a small windows service application in visual studio ..net 2003 which listens for incoming FTP files. These files would overwrite over time due to duplicate file names. However any time the windows service tries to rename the file (using flFile.move(source,destination)) it gets an error saying access is denied. The files are on the network, so we though the local system account may not have access We tried changing the...
3
9565
by: Chung Leong | last post by:
Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and MusicAlbum let you find MP3 and other music files based on the singer and album name; DocAuthor let you find Office documents created by a certain user; DocAppName let you find files of a particular program, and so on. Indexing Service uses plug-ins...
4
11744
by: sajid_yusuf | last post by:
Hi I am trying to develop a Windows service in VB.NET which has timer enabled and keeps checking a folder (or group of folders) for any new file or changed files. As soon as it detects any new file or changes (to files or folders) then it copies the files (and any new folders) to a folder in the mapped network drive. So far I have been successful in developing and deploying the service which works on my local machine with two drives...
4
1391
by: ags5406 | last post by:
Hi All I have a task that I'm thinking about attacking in two different ways but don't know enough about Windows Services. First: We have a server application written in VB.net that acts as a middleman between various Fortan DLL's (the calculation engines) and various front end client applications (one in Java and two in
4
8861
by: Steven De Smet | last post by:
Hello, This is my first post. I searched on the internet for answers but I was unable to solve my problem. So I hope that you guy's can help me with my VB.NET problem I tried to create a windows service that converts MS Word Files into .PDF files and after that we want to zip the .PDF files. Our code: Protected Overrides Sub OnStart(ByVal args() As String) ' Add code here to start your service. This method should set...
2
4506
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
Hi, I have 2 applications running, one Windows application project and the other windows services project. I want to call my Windows application in my windows services. I want to run them as seperate process. If my windows application starts running,only if it completes fully, then my windows services should continue its execution. My main process is Windows service.
1
4858
by: =?Utf-8?B?dmlzaHJ1dGg=?= | last post by:
Hi, I have 2 applications running, one Windows application project and the other windows services project. I want to call my Windows application in my windows services. I want to run them as seperate process. If my windows application starts running,only if it completes fully, then my windows services should continue its execution. My main process is Windows service.
5
3319
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name? Why do I need to set a property within my code to the service name? Are all these required or am I just doing this for consistency purposes?
0
9979
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11234
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10827
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10924
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7170
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4681
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.