Multithreading in PythonScript

Forum Forums Visual Components 4.1 Python Programming in VC Multithreading in PythonScript

This topic contains 3 replies, has 3 voices, and was last updated by Avatar BaiYang 5 months, 1 week ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #17149
    Avatar
    BaiYang
    Participant

    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.

    0
    #17158
    Avatar
    CaptainFeature
    Participant

    Hy,

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

    Why do you need it?

    Regards

    0
    #17153
    Avatar
    deadeye
    Participant

    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.

    0
    #17232
    Avatar
    BaiYang
    Participant

    @ 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.

    0
    #19323
    Avatar
    walemark
    Participant

    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.

     

    0
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.