I am trying to write to a client at the interval of every 1 second using Twisted protocol but when I use client.transport.write() twice, it prints two data on the same line. What I want to do is send first data to client then after certain delay send second data. Here is how : client.transport.write(list_of_commands[0]) time.sleep(10) client.transport.write(list_of_commands[1]) I also tried replacing client.transport.write() with client.transport.getHandle().sendall() as per the suggestion in Twisted transport.write, but it gave the same result.How can the problem be solved?
Asked
Active
Viewed 121 times
2
-
1The answer you linked warns that it might not be possible to do it reliably. Also, have you tried using reactor.callLater(60, somethingElse) instead of time.sleep as suggested in the comments? – Mirec Miskuf May 06 '16 at 09:35
-
The problem is that at times the above code works fine but not always. I have not tried reactor.calllater. Is "somethingElse" name of a function or we just use it as it is? – Telepresence May 11 '16 at 15:29
-
somethingElse is a placeholder for your function ;) You should write first list of command in on function and another set of commands in another function. If you use callLater instead of time.sleep, together with the global ACK after each write call, you might just manage what you are planning to do. – Mirec Miskuf May 11 '16 at 16:01
1 Answers
0
Twisted is asynchronous single threaded that is run by a main reactor loop, that means if you put a sleep in your code then the reactor is not doing it's job after period of times in your sleep.
You can use reactor.callLater(interval, self.transport.write, data)
.

adiga
- 34,372
- 9
- 61
- 83

Harianja Lundu
- 137
- 1
- 7