锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
classificationTitle: multiprocessing cannot spawn child from a Windows service
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 2.6
processStatus: open Resolution:
Dependencies: Superseder:
Assigned To: jnoller Nosy List: jnoller, orlenko (2)
Priority: normal Keywords patch
Created on 2009-02-06 02:00 by orlenko, last changed 2009-03-29 15:44 by jnoller.
Files
File name Uploaded Description Edit Remove
forking-patch orlenko, 2009-02-06 02:00 Patch of the forking module
Messages (1)
msg81247 - (view) Author: Volodymyr Orlenko (orlenko) Date: 2009-02-06 02:00
I think I've found a small bug with multiprocessing package on
Windows. If you try to start a multiprocessing.Process from a Python-
based Windows service, the child process will fail to run. When
running the parent process as a regular Python program, everything
works as expected.
I've tracked the problem down to how main_path is prepared in
multiprocessing.forking.get_preparation_data() (lines 370-377):
def get_preparation_data(name):
[...skipped a few lines...]
if not WINEXE:
main_path = getattr(sys.modules['__main__'], '__file__', None)
if not main_path and sys.argv[0] not in ('', '-c'):
main_path = sys.argv[0]
if main_path is not None:
if not os.path.isabs(main_path) and \
process.ORIGINAL_DIR is not
None:
main_path = os.path.join(process.ORIGINAL_DIR,
main_path)
d['main_path'] = os.path.normpath(main_path)
return d
When the program is running as a Windows service, but is not packaged
into a single executable, main_path will become the path to the
service executable (typically, pythonservice.exe). When this data
makes it to the child process, the prepare() function will treat
main_path as a path to a python module, and will try to import it.
This causes it to fail.
My quick-and-dirty solution was to check in get_preparation_data() if
main_path ends with '.exe', and if it does, to not pass it at all.
This solves the problem in my case, but perhaps there's a better way
to fix this? Here is my version of get_preparation_data():
def get_preparation_data(name):
'''
Return info about parent needed by child to unpickle process
object
'''
from .util import _logger, _log_to_stderr
d = dict(
name=name,
sys_path=sys.path,
sys_argv=sys.argv,
log_to_stderr=_log_to_stderr,
orig_dir=process.ORIGINAL_DIR,
authkey=process.current_process().authkey,
)
if _logger is not None:
d['log_level'] = _logger.getEffectiveLevel()
if not WINEXE:
main_path = getattr(sys.modules['__main__'], '__file__', None)
if not main_path and sys.argv[0] not in ('', '-c'):
main_path = sys.argv[0]
if main_path is not None:
if not os.path.isabs(main_path) and \
process.ORIGINAL_DIR is not
None:
main_path = os.path.join(process.ORIGINAL_DIR,
main_path)
if not main_path.endswith('.exe'):
d['main_path'] = os.path.normpath(main_path)
return d
鍦≒ython涓嬌鐢∕ySQL闇瑕佸畨瑁匨ySQLdb錛屼絾浠庡畼鏂圭珯鐐?nbsp;http://sourceforge.net/projects/mysql-python/ 鐩墠鍙兘涓嬭澆鍒板彧鎸丳ython2.5鐗堢殑MySQLdb銆傛湰鏂囦粙緇峎indows涓嬪畨瑁匨ySQLdb鐨勬柟娉曞拰甯歌闂澶勭悊銆?/tt>
Python2.6鍙互浠?a target=_blank>http://www.python.org/download
涓嬭澆瀹樻柟鐨勭増鏈畨瑁咃紝涔熷彲浠?a title=http://www.activestate.com/activepython/ target=_blank>http://www.activestate.com/activepython/涓嬭澆ActivePython瀹夎錛屽悗鑰呭湪windows涓嬩嬌鐢ㄦ洿鍔犳柟渚褲?/p>
MySQLdb鐗堟湰錛?MySQL-python-1.2.2.win32-py2.6.exe
涓嬭澆鍦板潃錛?a target=_blank>http://home.netimperia.com/files/misc/MySQL-python-1.2.2.win32-py2.6.exe
鍙傝錛?a target=_blank>http://sourceforge.net/forum/forum.php?thread_id=2316047&forum_id=70460
甯歌闂錛?br>1.ImportError: DLL load failed: 鎵句笉鍒版寚瀹氱殑妯″潡銆?br>----------------------------------------------------------------------------------------------------
D:\usr\local\Python26>python
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\usr\local\Python26\Lib\site-packages\MySQLdb\__init__.py", line 19, in <module>
import _mysql
ImportError: DLL load failed: 鎵句笉鍒版寚瀹氱殑妯″潡銆?br>----------------------------------------------------------------------------------------------------
瑙e喅鏂規(guī)硶錛氫笅杞絣ibmmd.dll(闄勪歡)鍜宭ibguide40.dll(闄勪歡)涓や釜dll鏂囦歡騫跺鍒跺埌python瀹夎鐩綍鐨凩ib\site-packages涓嬨?br>鍙傝錛?a target=_blank>http://sourceforge.net/forum/message.php?msg_id=5613887
2.ImportError: DLL load failed: 鎵句笉鍒版寚瀹氱殑妯″潡銆?br>----------------------------------------------------------------------------------------------------
D:\usr\local\Python26>python
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
D:\usr\local\Python26\lib\site-packages\MySQLdb\__init__.py:34: DeprecationWarning: the sets module is deprecated
from sets import ImmutableSet
----------------------------------------------------------------------------------------------------
瑙e喅鏂規(guī)硶錛?br>1) file "__init__", replace:
from sets import ImmutableSet
class DBAPISet(ImmutableSet):
with
class DBAPISet(frozenset) :
2) file "converters.py", remove:
from sets import BaseSet, Set
3) file "converters.py", change "Set" by "set" (IMPORTANT: only two places):
line 48: return set([ i for i in s.split(',') if i ])
line 128: set: Set2Str,
鍙傝錛?a target=_blank>http://sourceforge.net/forum/message.php?msg_id=5808948
浠g爜錛?
app = win32com.client.Dispatch("Word.Application")
print repr(win32com.client.constants.wdAlertsAll)
excel = win32com.client.Dispatch("Outlook.Application")
#win32com.client.gencache.EnsureDispatch('Outlook.Application')
print win32com.client.constants.msoAnimAccumulateAlways
榪愯緇撴灉錛?
File "chap1_0.py", line 87, in justfortest
print win32com.client.constants.msoAnimAccumulateAlways
File "C:\Python25\Lib\site-packages\win32com\client\__init__.py", line
168, in
__getattr__
raise AttributeError, a
AttributeError: msoAnimAccumulateAlways
榪欎釜閿欒鍑虹幇錛屾垜涔嬪墠涓鐩磋涓鴻皟鐢ㄤ簡(jiǎn)Dispatch涔嬪悗灝變細(xì)鑷姩鐨勫垱寤篶onstants鍙橀噺
鐨勶紝
鍚庢潵鎵嶇煡閬撲笉鏄繖鏍風(fēng)殑錛岃鑳藉浣跨敤constants錛屽繀闇瑕佷嬌鐢ㄤ笅闈㈢殑涓琛岃鍙?
win32com.client.gencache.EnsureDispatch('Outlook.Application')
鎴栬呮槸浣跨敤makepy,鍙互鎵撳紑pythonwin錛岀劧鍚庛怲ools銆戯紳銆嬨怌om MakePy Utility銆?
鏉?
鐢熸垚constants鐨勬枃浠?
4.鍙戠幇姝繪椿涓嶆垚鍔燂紝鏈鍚庡眳鐒跺彂鐜癶ttpd.conf灞呯劧娌℃湁淇敼銆?/p>
5.鍘熸潵鏄疺ista瀵規(guī)枃浠惰繘琛屼簡(jiǎn)淇濇姢錛屾病鏈夊啓榪涘幓錛侊紒錛侊紒錛佷簬鏄彁鏉儈~~
6.浣跨敤httpd.exe鎼炲畾浜?jiǎn)锛屼絾鏄敤ApacheMonitor.exe榪樻槸涓嶈錛屽嚭鐜?br>