Python3 Multitasking

This is the place for queries that don't fit in any of the other categories.

Python3 Multitasking

Postby DevynCJohnson » Sat May 25, 2013 1:56 pm

How can I have one or more lines of Python3.x code execute while the rest of the script continues? For example, I have some functions in the script that are time consuming and do not depend on each others tasks or output. I want these functions to execute at the same time and the script not wait for their completion. How can I do that?
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm

Re: Python3 Multitasking

Postby metulburr » Sat May 25, 2013 2:46 pm

Create a new thread for that task

Code: Select all
import threading
import time

def func():
    print('func start')
    time.sleep(3)
    print('func end')

threading.Thread(target=func).start()
print('continuing')
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1099
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Python3 Multitasking

Postby DevynCJohnson » Sat May 25, 2013 7:27 pm

I tried your suggestion, but I think I am doing wrong. What is the problem? I am using Python3.3.

Code: Select all
>>> def TEST():
...     subprocess('ls')
...
>>> threading.Thread(target=TEST).start()
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.3/threading.py", line 639, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.3/threading.py", line 596, in run
    self._target(*self._args, **self._kwargs)
  File "<stdin>", line 2, in TEST
TypeError: 'module' object is not callable
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm

Re: Python3 Multitasking

Postby setrofim » Sat May 25, 2013 7:33 pm

setrofim
 
Posts: 285
Joined: Mon Mar 04, 2013 7:52 pm

Re: Python3 Multitasking

Postby micseydel » Sat May 25, 2013 10:22 pm

Your problem there is much more basic than multi-tasking. When you import a module, you can't call it directly, you must use something inside of it. Perhaps you meant to use
Code: Select all
subprocess.check_out('ls')

instead of
Code: Select all
subprocess('ls')
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 929
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Python3 Multitasking

Postby DevynCJohnson » Sat May 25, 2013 11:58 pm

Of course. I should have caught that. Well, now that my ID-10-T error was found, the code works well.

Thank you everyone for all of your help.

New code that works:

Code: Select all
>>> def TEST():
...     subprocess.getoutput('notify-send "Hi"')
...
>>> threading.Thread(target=TEST).start()
Ubuntu 13.04 | Python3.3
User avatar
DevynCJohnson
 
Posts: 21
Joined: Sat May 25, 2013 1:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 1 guest