Home > Mobile >  Pyside2 threads
Pyside2 threads

Time:11-23

RT, check the Qt about Pyside2 related information, with the introduction of the QThread use or override the run method, no Qt main moveToThread method at present, I wrote a multithreaded according to own understanding to use Python, but the print result is not correct, have who can give me to find the problem, introduce?
MultiThreadTest: create a thread, groove trigger signal,
Worker: being moved to the new thread class
Specific code below
Worker:
 #! The/usr/bin/env python 
# encoding: utf-8
# @ author: qiaowei
# @ license: (C) Copyright 2013-2019
# @ contact: [email protected]
# @ file: worker. Py
# @ time: 2020/7/28 0028
# @ desc:


The import threading
The from PySide2. QtCore import QThread
The from PySide2. QtCore import Slot
The from PySide2. QtCore import QObject


The class Worker (QObject) :
Def __init__ (self) :
Super () __init__ ()
# print (' Worker. __init__='+ STR (id (QThread. CurrentThread ())))

@ Slot ()
Def do_something (self) :
# print (' Worker. Do_something='+ STR (threading. CurrentThread ()))
Print (' Worker. Do_something='+ STR (QThread. CurrentThread ()))


MultiThreadTest:
 #! The/usr/bin/env python 
# encoding: utf-8
# @ author: qiaowei
# @ license: (C) Copyright 2013-2019
# @ contact: [email protected]
# @ file: multithread_test. Py
# @ time: 2020/7/28 0028
# @ desc:

The import threading
The from PySide2. QtCore import QThread
The from PySide2. QtCore import QObject
The from PySide2. QtCore import Signal

The from multithread. Worker import worker


The class MultiThreadTest (QObject) :
Def __init__ (self) :
Super () __init__ ()
Self. __thread_a=QThread ()
Self. __worker=Worker ()
Self. Send_msg. Connect (self) __worker) do_something)
# pass

Def start_multithread (self) :
# print (' MultithreadTest. Start_multithread='+ STR (threading. CurrentThread ()))
Print (' MultithreadTest. Start_multithread='+ STR (QThread. CurrentThread ()))
Multi_thread=MultiThreadTest ()

Self. __worker. MoveToThread (self) __thread_a) thread ())
Self. __thread_a. Start ()
# self. __thread_a. Sleep (3)
# print (' MultithreadTest. Start_multithread='+ STR (threading. CurrentThread ()))
Print (' MultithreadTest. Start_multithread='+ STR (QThread. CurrentThread ()))
# multi_thread) send_msg) emit ()
The self) send_msg) emit ()
# print (' MultithreadTest. Start_multithread='+ STR (threading. CurrentThread ()))
Print (' MultithreadTest. Start_multithread='+ STR (QThread. CurrentThread ()))

# define parameterless signal channel
Send_msg=Signal ()


Run the code:
 #! The/usr/bin/env python 
# encoding: utf-8
# @ author: qiaowei
# @ license: (C) Copyright 2013-2019
# @ contact: [email protected]
# @ file: start_test. Py
# @ time: 2020/7/28 0028
# @ desc:


The from multithread. Multithread_test import MultiThreadTest


Start_01=MultiThreadTest ()
Start_01. Start_multithread ()


Results:
 MultithreadTest. Start_multithread=& lt; PySide2. QtCore. QThread object at 0 x0000000002fe6408 & gt; 
MultithreadTest. Start_multithread=& lt; PySide2. QtCore. QThread object at 0 x0000000002fe62c8 & gt;
Worker. Do_something=& lt; PySide2. QtCore. QThread object at 0 x0000000002fe62c8 & gt;
MultithreadTest. Start_multithread=& lt; PySide2. QtCore. QThread object at 0 x0000000002fe62c8 & gt;

CodePudding user response:

dddddd

CodePudding user response:

DDDDDDDDDD
  •  Tags:  
  • Qt
  • Related