Обсуждение: Does plpython support threading?

Поиск
Список
Период
Сортировка

Does plpython support threading?

От
Jinhua Luo
Дата:
Hi,

In my plpython function, I create new thread to do some routine works.
But I found the thread doesn't work, it would run a while, but
suddenly pause and stop working. For example, I write a test dead loop
to print something to file, but it would stop printing after some
time.

I am wondering whether plpython supports threading or not.

Regards,
Jinhua Luo



Re: Does plpython support threading?

От
Tom Lane
Дата:
Jinhua Luo <luajit.io@gmail.com> writes:
> In my plpython function, I create new thread to do some routine works.
> But I found the thread doesn't work, it would run a while, but
> suddenly pause and stop working. For example, I write a test dead loop
> to print something to file, but it would stop printing after some
> time.

> I am wondering whether plpython supports threading or not.

Creating multiple threads inside a Postgres backend is playing with fire.
You can make it work if you're very very careful, but what's more likely
to happen is you get burned.  None of the backend code is multithread
safe, so you must absolutely ensure that control never gets out of
libpython except in the main thread.
        regards, tom lane



Re: Does plpython support threading?

От
Jinhua Luo
Дата:
The new thread do not call any pg API or access any pg specific data.
The pg backend (main thread) puts string message into the python
Queue, and the new thread gets from the Queue and write to file, it's
simple and clear logic.

I test the same codes standalone, it works, but when I move them into
the plpython function. The new thread would be pause and seems freeze
(as said above, I even try to put a dead loop in the thread, it
doesn't run a dead loop actually).

So I guess it's somehow plpython specific limit?

Regards,
Jinhua Luo


2016-02-08 0:45 GMT+08:00 Tom Lane <tgl@sss.pgh.pa.us>:
> Jinhua Luo <luajit.io@gmail.com> writes:
>> In my plpython function, I create new thread to do some routine works.
>> But I found the thread doesn't work, it would run a while, but
>> suddenly pause and stop working. For example, I write a test dead loop
>> to print something to file, but it would stop printing after some
>> time.
>
>> I am wondering whether plpython supports threading or not.
>
> Creating multiple threads inside a Postgres backend is playing with fire.
> You can make it work if you're very very careful, but what's more likely
> to happen is you get burned.  None of the backend code is multithread
> safe, so you must absolutely ensure that control never gets out of
> libpython except in the main thread.
>
>                         regards, tom lane