Python Forum
Threading : class instance to be executed by a thread
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threading : class instance to be executed by a thread
#1
I want to create two threads and assign two class instances (object) to these threads.Then based on some condition, I will call the methods of those class (object methods) later. When I call class methods, will that method be executed in the thread which I created earlier?.
In Java it is done using Runnable interface. https://docs.oracle.com/javase/7/docs/ap...nable.html

import threading
from threading import Thread

class example(object):
test = On
def __init__(self, instance1, instance2):
    Thread(target =  instance1).start() # assign to first thread instnace1, the class instance
    Thread(target = instance2).start()  # assign to second thread instnace2, the class instance
    
def test(self):
    if test=='On':
Thread(target =  instance1).start() # assign to first thread instnace1, the class instance
    Thread(target = instance2).start()  # assign to second thread instnace2, the class instance

sorry i was not able to edit the post correctly. here is the sample code

1. create two threads and assign class instances
2. On callback function call the instance methods. The instance method should run in the threads created earlier.

Is it possible to run instance methods in two threads parallely on callback function?

import threading
from threading import Thread

class example(object):
test = On
def __init__(self, instance1, instance2):
    slef.instance1 = instance1
    slef.instance2 = instance2
    Thread(target = self.instance1).start() # assign to first thread instnace1, the class instance
    Thread(target = self.instance2).start() # assign to second thread instnace2, the class instance

#callback function to be called 
def callback(self):
    if example.test=='On':
       self.instance1.method1()  # call instance1 method on thread1
       self.instance2.method2()    # call instance2 method on thread2
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to not open the context menu if a mouse gesture is executed? MicheliBarcello 2 639 Aug-22-2023, 02:47 PM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,176 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  code not working when executed from flask app ThomasDC 1 836 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,716 May-03-2023, 08:22 AM
Last Post: billykid999
  Access instance of a class Pavel_47 5 2,035 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,076 Oct-01-2021, 08:32 PM
Last Post: muzikman
  Class Instance angus1964 4 2,407 Jun-22-2021, 08:50 AM
Last Post: angus1964
  Can we access instance variable of parent class in child class using inheritance akdube 3 13,889 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
  Issue referencing new instance from other class nanok66 3 2,180 Jul-31-2020, 02:07 AM
Last Post: nanok66
  Class variable / instance variable ifigazsi 9 4,221 Jul-28-2020, 11:40 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020