Giter Club home page Giter Club logo

uji_indoor_loc's Introduction

UJI_indoor_loc

How to create Py threads

Py threads are daemons (detached threads) unless the main programm doesn't wait for their completion.

Libraries

The main class thread:

import threading

Creating and running a simple thread

Just create the callack and then define the object thread.

import threading

# the "thread function"
def quellochevuoi_threadfunction( ... your args ... ):
	# really, everything you want inside it
	pass

# define the thread
t = threading.Thread( 
	target = ...your thread function ... ,
	args = ... a tuple of args ... ,
	daemon = ... default True ... ,
)

To run a thread use .start(), and .join() to wait it until end.

# start the thread
t.start( )

# wait the thread until it doesn't end
t.join( )

Creating many threads -- simple way

see this example, and in particular take into account the function threading.append( ...Thread obj ... ). The example just runs up to 4 threads giving to each of them a different integer index:

import threading
import time

def my_thread( index, name ):
	print( "I'm thread no.", index, "; my name is ", name )

names = [ "arturo", "ermenegildo", "alfredo", "bernadette" ]
wait_time = 2

for i in range(1, 5):
	# create the thread number 'i'
	t = threading.Thread(
		target = my_thread,
		args = ( index, names[i] ),
		daemon = True # detached thread, exit when the program ends
	)
	
	# append the thread 
	threading.append( t )
	
	# start the thread
	t.start( )

# wait until the end of the (universe) time
time.sleep( wait_time )

Return a value from a thread

just notice that .join( ) doesn't have a return value. If you want a return value the best way is to pass a reference to some memory location by argument. For instance, an object passed by reference make this work.

uji_indoor_loc's People

Contributors

fedehub avatar programmatoroseduto avatar

Watchers

 avatar

Forkers

fedehub

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.