1

I'm new to Python (and programming in general), so please be patient.

I am doing some lab equipment automation, and I am looking for a way to toggle power on one piece of equipment while taking data on another piece of equipment. I want these events to be asynchronous to each other, i.e. I want the power to toggle randomly during the data-taking process.

I've checked out the following:

-time.sleep--I was actually able to use this successfully on one setup, because the power supply was so slow to respond--I told it to shut off, then slept a random amount of time, then started taking data on the other equipment. This relies on the power supply always reacting much more slowly than the other piece of equipment, which will generally not be the case.

-multiprocessing/threading--I've been reading about this on SO and python.org, but I'm still not clear on whether this will accomplish what I want. I tried to test it but I'm finding it difficult to code, and I don't want to invest more time in it if it's nowhere near to what I want.

So, in a nutshell: Will multiprocessing do what I want? Is there any other way to do this?

6
  • 2
    You can have a look at Twisted: TwistedMatrix Commented Jul 10, 2012 at 23:33
  • Twisted is way cool but from the sound of things a simple thread should be fine. You didn't say anything about the the communication is taking place (http, socket, something else?). That could affect your solution. Commented Jul 10, 2012 at 23:35
  • 1
    just standard threading should work fine for this... no need for anything fancy Commented Jul 10, 2012 at 23:37
  • You may enjoy this book: amazon.com/Real-World-Instrumentation-Python-Acquisition/dp/… Commented Jul 10, 2012 at 23:50
  • 1
    Wait for the GIL trolls. Threads or separate processes are just fine for this task. Commented Jul 10, 2012 at 23:51

4 Answers 4

3

Can't think of any reason I wouldn't just write these as two totally separate scripts, since they don't demand any sharing of state.

Sign up to request clarification or add additional context in comments.

3 Comments

he can make a third one that launch both script!
Yes, or just a simple batch file/shell script.
I am not sure if I can make that work. I am using an iterator, created by someone else, that does something I am far from understanding. I have to stick pretty rigidly to a certain structure, or I get errors from the iterator. I'm sorry these statements are so vague, but that's the extent of my knowledge.
1

It seems you want a simple Thread model, where you create a thread for the task that you'd like to perform in the background (either the power toggle or the data taking). Here's an example (untested code):

from threading import  Thread                                                                                       

def take_data():
    while 1:                                                                            
        data = go_fetch_more_data()

data_thread = Thread(target=take_data)
data_thread.start()

while 1: 
    sleep_seconds = random.randint(0, 60)        
    time.sleep(sleep_seconds)
    toggle_power()

Note - with this approach the only way to stop the program is to kill it. If you need to monitor for a shutdown signal of some sort, you'll have to create a thread for each. Then you'll want to have a graceful way to shut down each thread. I'd provide an example, but I'm not totally clear on the Python best practice.

Comments

1

If you want two things to happen in parallel in Python, or most programming languages, then use threads. That's what they're for. Here is one tutorial on how to do threading in Python.

One thread will toggle power, and the other will take data.

Comments

0

asyncoro supports asynchronous event processing with coroutines. Programming with asyncoro is very similar to that of threads, except for using 'yield' for asynchronous completions. Your problem can be implemented with

import asyncoro, random

def collect(coro=None):
   while True:
      # collect data every 10 seconds?
      yield coro.sleep(10)
      data = read_data()

def toggle(coro=None):
    while True:
        yield coro.sleep(random.randint(10,30))
        toggle_power()

collector = asyncoro.Coro(collect)
toggler = asyncoro.Coro(toggle)

While threads are sufficient for this particular task, asyncoro framework provides many features, including support for concurrent, distributed, fault-tolerant programming. I suspect, given the domain of the problem, communicating with messages (channels, or point-to-point communication with coroutines) may be useful to you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.