| Server IP : 185.88.153.241 / Your IP : 216.73.217.115 Web Server : LiteSpeed System : Linux server312.bertina.biz 3.10.0-962.3.2.lve1.5.88.el7.x86_64 #1 SMP Fri Sep 26 14:06:42 UTC 2025 x86_64 User : ( 1405) PHP Version : 7.0.33 Disable Function : mail, apache_child_terminate, apache_setenv, define_syslog_variables, escapeshellarg, escapeshellcmd, exec, fp, highlight_file, ini_alter, ini_restore, inject_code, mysql_pconnect, openlog, passthru, phpAds_remoteInfo, phpAds_XmlRpc, phpAds_xmlrpcDecode, phpAds_xmlrpcEncode, popen, posix_kill, posix_mkfifo, posix_setpgid, posix_setsid, proc_close, proc_get_status, proc_nice, proc_open, proc_terminate, shell_exec, syslog, system, xmlrpc_entity_decode, show_source,dl,leak,crack_check,crack_closedict,crack_getlastmessage,crack_opendict,symlink,link,escapeshellarg,parse_ini_file, ln, show_source, pclose, parse_perms, mysql_list_dbs,stream_select,mysql_list_dbs,socket_select,socket_create,socket_create_listen,socket_create_pair,socket_listen,socket_accept,socket_bind,socket_strerror,socket_clear_error,socket_close,socket_connect,socket_get_option,socket_getpeername,socket_getsockname,socket_last_error,socket_read,socket_recv,socket_recvfrom,socket_send,socket_sendto,socket_set_block,socket_set_nonblock,socket_set_option,socket_shutdown,socket_write,readlink,pfsockopen,pcntl_exec,pcntl_fork,pcntl_signal,pcntl_waitpid,pcntl_wexitstatus, pcntl_wifexited, pcntl_wifsignaled, pcntl_wifstopped,pcntl_wstopsig,pcntl_wtermsig,fpassthru, posix_access, posix_ctermid, posix_errno, posix_get_last_error, posix_getcwd, posix_getgrnam, posix_getgroups, posix_getlogin, posix_getpgrp, posix_getpwnam, posix_getpwuid, posix_getrlimit, posix_getsid, posix_initgroups, posix_isatty, posix_mknod, posix_setegid, posix_seteuid, posix_strerror, posix_times, posix_ttyname, diskfreespace, disk_free_space, disk_total_space, sys_getloadavg,get_current_user MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /lib64/python2.7/Demo/threads/ |
Upload File : |
# Generator implementation using threads
import sys
import thread
class Killed(Exception):
pass
class Generator:
# Constructor
def __init__(self, func, args):
self.getlock = thread.allocate_lock()
self.putlock = thread.allocate_lock()
self.getlock.acquire()
self.putlock.acquire()
self.func = func
self.args = args
self.done = 0
self.killed = 0
thread.start_new_thread(self._start, ())
# Internal routine
def _start(self):
try:
self.putlock.acquire()
if not self.killed:
try:
apply(self.func, (self,) + self.args)
except Killed:
pass
finally:
if not self.killed:
self.done = 1
self.getlock.release()
# Called by producer for each value; raise Killed if no more needed
def put(self, value):
if self.killed:
raise TypeError, 'put() called on killed generator'
self.value = value
self.getlock.release() # Resume consumer thread
self.putlock.acquire() # Wait for next get() call
if self.killed:
raise Killed
# Called by producer to get next value; raise EOFError if no more
def get(self):
if self.killed:
raise TypeError, 'get() called on killed generator'
self.putlock.release() # Resume producer thread
self.getlock.acquire() # Wait for value to appear
if self.done:
raise EOFError # Say there are no more values
return self.value
# Called by consumer if no more values wanted
def kill(self):
if self.killed:
raise TypeError, 'kill() called on killed generator'
self.killed = 1
self.putlock.release()
# Clone constructor
def clone(self):
return Generator(self.func, self.args)
def pi(g):
k, a, b, a1, b1 = 2L, 4L, 1L, 12L, 4L
while 1:
# Next approximation
p, q, k = k*k, 2L*k+1L, k+1L
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
# Print common digits
d, d1 = a//b, a1//b1
while d == d1:
g.put(int(d))
a, a1 = 10L*(a%b), 10L*(a1%b1)
d, d1 = a//b, a1//b1
def test():
g = Generator(pi, ())
g.kill()
g = Generator(pi, ())
for i in range(10): print g.get(),
print
h = g.clone()
g.kill()
while 1:
print h.get(),
sys.stdout.flush()
test()