473,666 Members | 2,114 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 2653
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
15217
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
5133
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
4442
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
9548
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
11728
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
1377
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
8831
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
4483
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
4844
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
3296
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
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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
8781
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
8551
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
8640
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
7386
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
4198
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...
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.