Python (Django) running code in main thread

时间:2018-09-01 22:53:22

标签: python django multithreading python-multithreading python-requests-html

I am trying to use requests_html to parse HTML page that requires javascript rendering. And, I've found out that using this library (my last refuge) requires processing in main thread ONLY.

The thing is that I get the request to start the whole process in a Django view request, which is spawned in a seperate thread than the UI thread, which makes things complicated.

It seems there is no well defined way of spawning things in main thread from another! (As there is in Android (Java) for example: activity.runOnUiThread( callback...)).

Python provides loops and queues, but I could find a little explanation of how to signal Python (Django's?) main loop to run a callback. Not to talk about waiting for it to finish in that other thread...

I am sure there is a method of doing so without invoking a whole seperate process (AKA multiprocessing) - I know this option. It is a heavy solution which I do use for other heavier tasks, but it requires transfering data back/forth out of the child process in ugly ways.

So, what would be the simplest way to invoke code in main/UI thread of my Django server?

0 个答案:

没有答案