Python Forum
How to do multithrading of a simple function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do multithrading of a simple function?
#2
#!/usr/bin/python3
import threading, time
from random import *

class cal():
    def __init__(self, i, a, b):
        self.i = i
        self.a = a
        self.b = b
    def add(self, c):
        print("thread", self.i, "starts", self.a, self.b, c)
        time.sleep(2)
        s = self.a + self.b + c
        print("thread", self.i, "ends: ", self.a, self.b, c, "sum: ", s)

for i in range(10):
    a = randint(1, 10)
    b = randint(1, 10)
    c = randint(1, 10)
    obj = cal(i, a, b)
    t = threading.Thread(target=obj.add, args=(c,))
    t.start()
Reply


Messages In This Thread
RE: How to do multithrading of a simple function? - by heiner55 - Nov-17-2017, 02:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  got SyntaxError while building simple function zarize 2 2,145 Feb-14-2020, 10:51 AM
Last Post: zarize
  Simple statistics with range function Pythonlearner2019 2 2,148 Nov-25-2019, 05:25 PM
Last Post: Pythonlearner2019
  Cannot get simple i/o to function. jerryi 10 6,785 Jul-27-2019, 06:22 PM
Last Post: jerryi
  Need help with a simple function WorldPark 4 2,676 Apr-26-2019, 12:28 PM
Last Post: perfringo
  Why this simple function doesnt work? blackknite 8 4,035 Jan-05-2019, 12:32 PM
Last Post: buran
  Simple Function Problem, please help ShadowWarrior17 16 7,127 Jan-03-2018, 09:29 PM
Last Post: ShadowWarrior17
  Simple Function Call PappaBear 2 3,188 Apr-04-2017, 11:12 PM
Last Post: PappaBear

Forum Jump:

User Panel Messages

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