python - hvorfor løkken løber ikke ordentligt

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg forstår ikke, hvorfor sløjfen ikke kører mit Python script.


import gtk, sys
from ctypes import *

PIPE\_ACCESS\_DUPLEX = 0x3
PIPE\_TYPE\_MESSAGE = 0x4
PIPE\_READMODE\_MESSAGE = 0x2
PIPE\_WAIT = 0
PIPE\_UNLIMITED\_INSTANCES = 255
BUFSIZE = 9600
NMPWAIT\_USE\_DEFAULT\_WAIT = 0
INVALID\_HANDLE\_VALUE = -1
ERROR\_PIPE\_CONNECTED = 535

MESSAGE = "Default answer from server"
szPipename = "\\.\pipe\mynamedpipe"

def ReadWrite\_ClientPipe\_Thread(hPipe):
    chBuf = create\_string\_buffer(BUFSIZE)
    cbRead = c\_ulong(0)
    while True:
        fSuccess = windll.kernel32.ReadFile(hPipe, chBuf, BUFSIZE, byref(cbRead), None)
        if ((fSuccess ==1) or (cbRead.value != 0)):
            messageArd = chBuf.value
            cbWritten = c\_ulong(0)
            fSuccess = windll.kernel32.WriteFile(hPipe,c\_char\_p(MESSAGE), len(MESSAGE), byref(cbWritten), None )
    windll.kernel32.FlushFileBuffers(hPipe)
    windll.kernel32.DisconnectNamedPipe(hPipe)
    windll.kernel32.CloseHandle(hPipe)
    return 0

def main(): 
    THREADFUNC = CFUNCTYPE(c\_int, c\_int)
    thread\_func = THREADFUNC(ReadWrite\_ClientPipe\_Thread)
    while True:
        print "Print test" #This should be repeated
        hPipe = windll.kernel32.CreateNamedPipeA(szPipename,PIPE\_ACCESS\_DUPLEX,PIPE\_TYPE\_MESSAGE | PIPE\_READMODE\_MESSAGE | PIPE\_WAIT,PIPE\_UNLIMITED\_INSTANCES,BUFSIZE, BUFSIZE,NMPWAIT\_USE\_DEFAULT\_WAIT,None)
        fConnected = windll.kernel32.ConnectNamedPipe(hPipe, None)
        if ((fConnected == 0) and (windll.kernel32.GetLastError() == ERROR\_PIPE\_CONNECTED)):
            fConnected = 1

        if (fConnected == 1):
            dwThreadId = c\_ulong(0)
            hThread = windll.kernel32.CreateThread(None, 0,thread\_func, hPipe, 0, byref(dwThreadId))

    gtk.main() 

if \_\_name\_\_ == "\_\_main\_\_": 
    main()


Dette bør gentages:


 print "Print test"


Hvis jeg ændrer def Main eller fjern Named Pipe script, løber løkken korrekt.


def main(): 
    while True:
        print "Print test" #This should be repeated
    gtk.main() 


Kan nogen hjælpe mig med at løse dette problem? Tak skal du have

Bedste reference


Løst problem.


Looping virker ikke, fordi der er to løkker, der kører samtidigt, løsningen med multithreadning. At køre to løkker i samme retning