Вопрос

EDIT Apparently this goes deeper than I thought; I just attempted to create the server inside the running thread, to no avail.

Running Python2.7 32 bit Windows. It would appear that the dde module (or the way I am using it?) does not play nicely with threads.

Here's stripped down code to demonstrate:

This Works:

import win32ui
import dde
import threading

class Conversation(threading.Thread):
    def __init__(self, server, first, second, tag)
        threading.Thread.__init__(self)
        self.tag = tag #string
        self.first = first #string
        self.second = second #string
        self.server = server #dde server object

        self.conversation = dde.CreateConversation(server)

        #The focus of the problem. Here it works.
        self.conversation.ConnectTo(self.first, self.second)

    def run(self):
        print ""

def main():
    machine = "Irrelevant_MachineName"
    tag = "Irrelevant_Tagname"
    server = dde.CreateServer()
    server.Create("Irrelevant_ServerName")

    t = Conversation(server, "Irrelevant_Name", machine, tag)
    t.start()


main()    

This doesn't:

class Conversation(threading.Thread):
    def __init__(self, server, first, second, tag):
        threading.Thread.__init__(self)
        self.tag = tag #string
        self.first = first #string
        self.second = second #string
        self.server = server #dde server object

    def run(self):
        self.conversation = dde.CreateConversation(server)

        #Focus of problem.
        #Inside here it does not work.
        self.conversation.ConnectTo(self.name1, self.name2)

def main():
    machine = "Irrelevant_MachineName"
    tag = "Irrelevant_Tagname"
    server = dde.CreateServer()
    server.Create("Irrelevant_ServerName")

    t = Conversation(server, "Irrelevant_Name", machine, tag)
    t.start()


main()

When it fails, I get this error:

Exception in thread Thread-1:
Traceback (most recent call last):
    File "c:\Python27\lib\threading.py", line 808, in __bootstrap_inner
     self.run()
    File "DDE.py", line 28 in run
     self.conversation.ConnectTo(self.first, self.second)
error: ConnectTo failed

Why would this be? This is going to be my first multithreaded program, so I'm not sure if I'm doing something stupid here.

But to me it seems perfectly reasonable that I should be able to call the dde.server.conversation object's ConnectTo method from within the threading module's "run()" method.

I'd looked into multiprocessing, but I don't think that's as applicable to my situation.

So, any ideas? I would greatly appreciate the help!!!

edit added in "threading.Thread.init(self)" to both failing and succeeding examples. Still no dice.

edit 2

Also, I've heard tales of the tcl dde library, but know not where to find it. If anybody could provide me with a way to access it in python, I would be forever in your debt.

Это было полезно?

Решение

So it appears that with Windows DDE you're limited to using an instance of ConnectTo inside of the thread itself. It does support multithreading, and while your run is still inside of your class it's possible that DDE sees that as outside your initial thread. Don't know that that's actually an answer, but it seems to me that if you're first block of code is working it's entirely possible that this error is just an idiosyncrasy of Windows DDE and multithreading.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top