473,546 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

threads

Hi

I have create two threads (from threading module). I want to
synchronize this two in the folowwing way

def Threa1func():
do stuff..
while something
if test:
CHANGE TO Thread2
do stuff...

def Thread2func():
do stuff ...
CHANGE TO Thread1

Thread1 = threading.Threa d(target=Thread 1func)
Thread2 = threading.Threa d(target=Thread 2func)

them i've started this two threads

Thread1.start()
Thread2.start()

I want to know if it is posible to do this CHANGE of threads.

thanks in advance

Zunbeltz
--
Remove XXX from email: zu******@wm.lc. ehu.esXXX
Jul 18 '05 #1
3 2118
Zunbeltz Izaola wrote:
Hi

I have create two threads (from threading module). I want to
synchronize this two in the folowwing way


Maybe you don't want threads but coroutines. Google for it, and here's one page
on it: http://www-106.ibm.com/developerwork.../l-pythrd.html
--
--
Every sufficiently advanced magic is indistinguishab le from technology
- Arthur C Anticlarke
Jul 18 '05 #2
Zunbeltz Izaola wrote:

I have create two threads (from threading module). I want to
synchronize this two in the folowwing way

def Threa1func():
do stuff..
while something
if test:
CHANGE TO Thread2
do stuff...

def Thread2func():
do stuff ...
CHANGE TO Thread1

Thread1 = threading.Threa d(target=Thread 1func)
Thread2 = threading.Threa d(target=Thread 2func)

them i've started this two threads

Thread1.start()
Thread2.start()

I want to know if it is posible to do this CHANGE of threads.


Not exactly, or yes, depending on what you really want to do.

Can you describe the problem in different terms, without reference
to actual Python code or the threading module? It's not at all clear
that you really want to use threads for your problem, and if you
do, you are looking for a different concept than "CHANGE"... maybe
semaphores or something.

-Peter
Jul 18 '05 #3
[Zunbeltz Izaola]
I want to know if it is posible to do this CHANGE of threads.


It is not possible to "switch" execution between threads like this:
the whole point of threads is that there multiple execution contexts
operating in parallel.

Once a thread has been started, it must run all the way through until
it's execution terminates naturally. You cannot even raise exceptions
in one thread from another thread.

What you *can* do is communicate information between threads. This is
most often done by using a Queue object, which you can read about here

http://www.python.org/doc/current/lib/module-Queue.html

So a good design pattern for what (I think) you're trying to do is to
devise a class/object which represents the work to be done, and send
that "work object" back and forth on the Queue between the threads.
This way, it "appears" that the work is actually moving between
threads. When there is no work for a thread to do, it is blocked in a
Queue.get call, waiting for something to do. But it does not "go
away".

Of course, this is just one way to do it: another poster suggested
another excellent approach: co-routines.

HTH,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5380
by: Ronan Viernes | last post by:
Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import thread, sys
0
1998
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux distributions using the Native Posix Threading Layer (NPTL), this isn't entirely true anymore and is AFAIK unsupported (using a pid to signal/identify...
6
3179
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to...
34
10747
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; public class A {
3
22155
by: bygandhi | last post by:
Hi - I am writing a service which will check a process and its threads for their state ( alive or dead ). The process has 5 .net managed threads created using thread.start and each have been assigned a name. As in .net there is no way we can get the managed threads, I am thinking of making a win32 call and check their status.
10
1661
by: [Yosi] | last post by:
I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes, OS execute (All have same priority) Thread#1 may be other threads, Thread#2 may be other threads, Thread#3 may be other threads,
6
2506
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the data is collected is that we have different schedules (so one set of data is collected say every second, another set of data might be collected every...
3
5956
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional ThreadPool thread. And that is exactly what MS VIsual Studio shows. But when I run Processexplorer or Taskmanager I see 2 additional threads,...
10
1739
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that T2, T4 and T5 are already running. Thanks, Darian
4
2248
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should start five threads and process five files at a time. Is this a good idea? I picked the number five at random... I was thinking that I might check...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7794
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.