Multithreading in PythonScript

Hello,

i have wrote a multithreading program in PythonScript using “thread” module,but it shows a error dialog as follow:

the code as follow:

from vcScript import *
import thread

def f1():
while True:
delay(1)
print(’-------------thread1---------------’)
print(‘thread1’)
def f2():
while True:
delay(1)
print(’-------------thread2---------------’)
print(‘thread2’)

def OnRun():
thread.start_new_thread(f1,(‘thread1’,1))
thread.start_new_thread(f2,(‘thread2’,2))
delay(100)

Does anyone know how to solve this problem?

Thanks for your help.

I guess you’re using the module thread, which should not exposed to the user.
In normal CPython 2.7 it is a very low level implementation and should not be used (Python 2.7 aswell).

Here is an example from Stackless Python 2.7. This implementation of Python is used in VC. The usage differs a little bit. You can have “channels” which are a kind of message queue and schedulers.

https://stackless.readthedocs.io/en/2.7-slp/library/stackless/threads.html

Try as first if the provided examples are working.

Hy,

i think Multithreading is not supported in Python-Scripts of components…

Why do you need it?

Regards

@ Captain_Feature

I want to execute two parallel tasks in one python script,because the two tasks share the same resource .

@deadeye

Thanks for your reply,i will have a try.

The Python Global Interpreter Lock or GIL, in simple words, is a mutex (or a lock) that allows only one thread to hold the control of the Python interpreter. All the GIL does is make sure only one thread is executing Python code at a time; control still switches between threads. What the GIL prevents then, is making use of more than one CPU core or separate CPUs to run threads in parallel.

Python threading is great for creating a responsive GUI, or for handling multiple short web requests where I/O is the bottleneck more than the Python code. It is not suitable for parallelizing computationally intensive Python code, stick to the multiprocessing module for such tasks or delegate to a dedicated external library. For actual parallelization in Python, you should use the multiprocessing module to fork multiple processes that execute in parallel (due to the global interpreter lock, Python threads provide interleaving, but they are in fact executed serially, not in parallel, and are only useful when interleaving I/O operations). However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously.