Support for Python threads

Hi,

I’m curious what the support for Python threads is in 088.

For example, the class myhttp below spawns an http server correctly from the Python executable used by the 088 installation, but inside of Touch does not seem to work. (Doesn’t crash, but doesn’t work, either.)

I tried this snippet in the python textport:

from myhttp import myhttp S = myhttp.myhttp() S.start()

contents of myhttp.py -

import threading from http.server import * class myhttp: def run(self, server_class=HTTPServer, handler_class=BaseHTTPRequestHandler): print("Start run") server_address = ('', 8000) httpd = server_class(server_address, handler_class) httpd.serve_forever() print("After serve_forever") def start(self,): threading.Thread(target = self.run).start()

Another example that fails silently:

import threading def foo(): print("Good morning.") t = threading.Timer(2, foo) t.start()

Thanks for any feedback.

Jeff Burke

Hi Jeff.

One thing I notice, your last simple printing example does work.
Its just that you have to wait the full two seconds before pressing enter in the textport to see the output. Thats because in most cases, the printed statements are pulled when you press enter, not pushed as they occur.

That being said, multi threaded printing may be a little dangerous in TouchDesigner. We can address that.

However threading of TouchDesigner objects IS NOT supported (ie, changing parameters, adding points, etc). This all has to be done in the main single thread.

Finally, not sure about your previous examples, I’ll have a look, but generally speaking it does seem python threading is working as expected.

Cheers,
Rob

Hi Rob,

Thanks. Indeed, as you point out, the second example works if you press enter in the Textport after a few seconds. With that in mind, I tried the http server example again and was able to get a little further:

If I attempt to load localhost:8000 in a browser and hit enter in the Textport as the page was loading, I got the normal “Unsupported method” log messages for the default server, and then an exception:

[code]
python >>>
jbMB15 - - [29/Oct/2012 20:09:03] code 501, message Unsupported method (‘GET’)
python >>>
python >>>
jbMB15 - - [29/Oct/2012 20:09:03] “GET / HTTP/1.1” 501 -
python >>>
python >>>

Exception happened during processing of request from (‘127.0.0.1’, 55527)
Traceback (most recent call last):
File “C:\Program Files\Derivative\TouchDesigner088 64-Bit\bin\lib\socketserver.py”, line 284, in _handle_request_noblock[/code]

Perhaps this is related to the way output is being handled in the Textport?

(Also, there was a syntax error in what I posted to the board, should be S = myhttp() , but that was a just incorrectly transposed from my own code.)

I realize the issues related to having other threads impact Touch variables and the render loop, but would like to experiment a bit with multithreading for handling network and other async operations within python. I’ve made similar things work in the C++ and Python versions of Ogre, for example, with very careful handling of locks, etc. and it was handy for interactive installation work.

Thanks,
Jeff

The next build will have a fix to make printing thread safe,
as well as regular import statements.
The http example above causes TouchDesigner to eventually crash as the thread was
conflicting with our DAT import mechanism.

That being said, the ‘GET’ method problem you see is probably unrelated to this.
When I now run S.start() it seems to work the same in TouchDesigner as a python shell.
ie, it simply says “Start run”.

Cheers
Rob.

Hi Rob,

Excellent - thanks for the update. (And yes, the ‘GET’ error is not related…)

Jeff

Is threading possible now? (12710)

I’m following these exact steps (thanks Jeff!), and if I don’t thread, then the server runs, but TouchDesigner locks up completely (well, it’s running, but all GUI is frozen).

That implies to me that the code is fairly workable, and the firewall isn’t an issue (I have TD’s python allowed to connect).

When I run with the threading active, the server starts - I see the message - but I can’t get to the server.

Is there still a conflict with threading in touch?

Bruce

Hmm, oops. I see from this that I won’t be able to get at my goal anyway:

viewtopic.php?f=4&t=5396&p=19904&hilit=threading#p19904

The idea was to serve data pulled from table DATs.

I could probably work with Malcolm’s queue idea, if threading worked with a server.

I’m trying single shot now - calling handle_request() once a frame.

Could you save the DATs to a file and read the file via python, do your work, save over the file, then refresh the DAT in TouchDesigner?

Sure, but you can just pass a string to the thread too. No need to have it out to disk.

I got it working with handle_request and queues. Any clue why serve_forever would not work? The minimum timeout in handle_request seems to be too
Long to keep a decent frame rate.